1

Why if I call:

http://localhost:55058/support/Faqs

I get this:

http://localhost:55058/Products/Faqs/Faq

with the following RegisterRoutes implementation? How can I get:

http://localhost:55058/Support/Faqs/Faq

Thanks.

public class SupportController : Controller
{
    public ActionResult FAQs()
    {
        return RedirectToAction("Faq", "Faqs");
    }
}

public static void RegisterRoutes(RouteCollection routes)
{

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.IgnoreRoute("{filename}.html|js|css|gif|jpg|jpeg|png|swf");

        routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
        routes.IgnoreRoute("{resource}.html/{*pathInfo}");

        routes.MapRoute(
            "Products", // Route name
            "Products/{controller}/{action}", // URL with parameters
            new { controller = "ProductName", action = "Index" } // Parameter defaults
            );

        routes.MapRoute(
         "Support", // Route name
         "Support/{controller}/{action}", // URL with parameters
         new { controller = "Support", action = "Index" } // Parameter defaults
         );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );

}
abenci
  • 8,422
  • 19
  • 69
  • 134
  • what happens if you comment the first "Product" route and try the url??? – K D Mar 06 '13 at 13:15
  • It works fine but I need even the `Products` route to allow the `http://localhost:55058/Products/ProductName/Intro` URL to work – abenci Mar 06 '13 at 13:18
  • I think everything is working fine, just the line **return RedirectToAction("Faq", "Faqs");** is making some mess.. can you please write **return Content("TEST");** instead of that to check if i am right? – K D Mar 06 '13 at 13:24
  • it works in the same way but the URL shown is `http://localhost:55058/Products/faqs/Faq` instead of `http://localhost:55058/Support/faqs/Faq` – abenci Mar 06 '13 at 13:52

1 Answers1

2

Without a bit more data its hard to tell - but it looks like you're kind of trying to use areas, but not actually using areas?

What's happening is your RedirectToAction("Faq", "Faqs") is matching all of your routes - so it picks the first one, in this case Products.

What's the difference between "Products/{controller}/{action}" and "Support/{controller}/{action}" routes? Are they coming from a different place? You're sending them both to the same controller, same action regardless of what the URL starts with - and that seems confusing.

If by going to /products/... you should get different results than /support/... you should look into MVC Areas.

If they're supposed to get the same results which is what you have now - maybe you should consider removing those urls and just use the default?

Here's some additional resources on areas:

  1. From MSDN
  2. From Asp.net
  3. From Stack Overflow

EDIT From your comment:

To route: http://localhost:55058/Products/ProductName/Download

Update your routes like this:

 routes.MapRoute(
            "Products", // Route name
            "Products/ProductName/{action}", // URL with parameters
            new { controller = "ProductName", action = "Index" } // Parameter defaults
            );

        routes.MapRoute(
         "Support", // Route name
         "Support/{controller}/{action}", // URL with parameters
         new { controller = "Support", action = "Index" } // Parameter defaults
         );
Community
  • 1
  • 1
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
  • Hi Mark, yes I am trying to mimic areas. I send the `"Products/{controller}/{action}"` to the ProductNameController and the `"Support/{controller}/{action}"` to the SupportController... – abenci Mar 06 '13 at 13:16
  • Updated my answer - but I'm a bit confused by the route you want. If you want: Products/Products (controller) / Index (action) ? - Products/Products/action, I can change the routes to that. And Support/Support/Action? Support/Faqs/Faqs doesn't make sense in that context - because you're going to Support/Faqs (do you have a faqs controller)/Faq – Mark Oreta Mar 06 '13 at 13:21
  • The suggested solution makes the Products route to fail... We call it with the following actions `http://localhost:55058/Products/ProductName/Gallery`, `http://localhost:55058/Products/ProductName/Download`, etc. – abenci Mar 06 '13 at 13:54
  • I've updated the answer for the routes above - but what about http://localhost:55058/Support/Faqs/Faq. Would that need to Goto the Faqs controller, or the Support controller? – Mark Oreta Mar 06 '13 at 13:56
  • We have a FaqsController with the `Faq` action – abenci Mar 06 '13 at 14:06
  • Not completely, `Products/ProductName/Downloads` does a `RedirectToAction("Download", "Download")` that is displayed as `http://localhost:55058/Support/Download` instead of `http://localhost:55058/Products/ProductName/Download` – abenci Mar 06 '13 at 14:15
  • Should: RedirectToAction("Download", "Download") being going to the ProductName Controller, Download Action? If so - your redirect should be: RedirectToAction("Download") or RedirectToAction("Download", "ProductName") – Mark Oreta Mar 06 '13 at 14:19
  • No, sorry I checked. It's `RedirectToAction("Index", "Download")` – abenci Mar 06 '13 at 14:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25697/discussion-between-mark-oreta-and-alberto) – Mark Oreta Mar 06 '13 at 14:30