50

I am having trouble with Html.ActionLink when I have a route that takes more than one parameter. For example, given the following routes defined in my Global.asax file:

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

routes.MapRoute(
    "Tagging",
    "{controller}.mvc/{action}/{tags}",
    new { controller = "Products", action = "Index", tags = "" }
);

routes.MapRoute(
    "SlugsAfterId",
    "{controller}.mvc/{action}/{id}/{slug}",
    new { controller = "Products", action = "Browse", id = "", slug = "" }
);

The first two routes work without a problem, but when I try to create an action link to the third route using:

<%= Html.ActionLink(Html.Encode(product.Name), "Details", new { id = product.ProductId, slug = Html.Encode(product.Name) }) %>

I end up with a URL like [site-root]/Details/1?slug=url-slug whereas I would like the URL to be more like [site-root]/Details/1/url-slug

Can anyone see where I am going wrong?

2
  • 2
    I've always had this kind of issue and never found an acceptable solution other than hard-coding the controller and action names. Leads to LOTS of routes.
    – user1228
    Apr 9, 2009 at 13:37
  • you could look at the restful routing for ASP.NET MVC project stevehodgkiss.com/2009/10/11/… I use this and can achieve urls such as /blogs/1/posts/2/comments/3
    – Paul
    Jan 28, 2010 at 17:02

3 Answers 3

60

It is using the first route that is fully satisfied. Try putting your SlugsAfterId route above the Default one.

It's basically going: "Check Default. Got an action? Yes. Got an id? Yes. Use this one and chuck any other parameters in the querystring."

As a side note, doing that will make your Default route redundant as you provide a default value for the slug parameter.

32

Garry (above) is correct. You can use Mr. Haack's route debugger for MVC. It can help resolve routing issues by showing you which routes are hit and when.

Here is the Blog Post. And here is the Zip File.

0
8

You could add a Constraint to your Routes that contain "id" since it's presumably only to accept a number. This way the first route will only match when the "id" is numeric, then it would make the second route for all other values. Then place the one that includes the {slug} at the top and everything should work correctly.

routes.MapRoute(
    "SlugsAfterId",
    "{controller}.mvc/{action}/{id}/{slug}",
    new { controller = "Products", action = "Browse", id = "", slug = "" },
    new { id = @"\d+" }
);

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}.mvc/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" },  // Parameter defaults
    new { id = @"\d+" }
);

routes.MapRoute(
    "Tagging",
    "{controller}.mvc/{action}/{tags}",
    new { controller = "Products", action = "Index", tags = "" }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.