ASP.NET MVC: Route order is really important

If you have multiple MVC Routes, check carefully the route order! If a less specific rule comes first, it will be threated first.

25 January 2011
Christoph Keller Christoph Keller

If you have multiple MVC 2 Routes, check carefully the route order! If a less specific rule comes first, it will be threated first.

For an example, I have a Language-Navigation Route:

routes.MapRoute(
    "Language_Switch",
    "language/{language}",
    new { controller = "Language", action = "Index" }
);

and the default Route:

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

If the default route would come first, it would threat a specific language route as default too. This affects the method Html.ActionLink() too, because it would resolve the url according the known Routes!

So just be careful! :)


comments powered by Disqus