views:

903

answers:

2

I have a problem with the RC1 version of ASP.Net MVC. Whenever I add a Route before the "Default" route, the resulting Urls created are for the first Route added.

Here is my Routing in Global.asax.cs

routes.MapRoute(
            "product-detailed",
            "Products/{controller}/{action}/{id}",
            new { controller = "ProductSubType", action = "Index", id = "" }
        );

        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  
        );

My Url creation:

        <%= Html.ActionLink("Bikes", "Index", "Bikes") %><br />
        <%= Html.RouteLink("Bikes", "product-detailed", new { controller = "Bikes", action = "Index" }) %>

I would expect the first ActionLink to create a Url like "/Bikes/Index" and the second RouteLink to create "/Products/Bikes/Index", but both Urls end up as "/Products/Bikes/Index".

What am I missing here on the routing?

Thanks.

+3  A: 

You're not missing anything. It's working as designed.

Since the controller and action are both variable in the top route, with no limitations on valid values, then that route is valid for all values of controller and action.

Potential work-arounds:

  • Fix the controller and/or action values so that they're not part of the URL
  • Add restrictions for the top route for values of controller and/or action
  • Always use route links instead of action links, since they unambiguously state which route is the correct route.
Brad Wilson
doh! was just typing my response... :P
Brannon
(If anybody was thinking I was being a jerk to Brannon, I know him in real life.)
Brad Wilson
A: 

Brad,

Thank you for the ideas. I was able to take a look at some other samples where the names of the route values are different and I can get the routing working correctly.

I agree with the idea of using the Html.RouteLink and Url.RouteLink methods for creating Urls.

Thanks again,

Cole

Cole B