Here is necessary code to reproduce a very strange problem with ASP.NET MVC 3.0 routing:
Route registration in Global.asax.cs:
routes.MapRoute("History", "Customer/History", new {controller = "User", action = "History", someParam = UrlParameter.Optional});
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Here we declare a route to the user's history. But in the URL we want "Customer" instead of "User". Also please note parameter someParam. Controller User does really exist and has action History.
Now usage in view:
<a href="<%= Url.Action("History", "User") %>">History</a>
<a href="<%= Url.Action("History", "User", new { someParam="qqq" }) %>">History with param</a>
I am using here Url.Action() instead of Html.ActionLink() only for clarity.
And here is the result - how this part of the view was rendered:
<a href="/Customer/History">History</a>
<a href="/User/History?someParam=qqq">History with param</a>
Now the problem is clear - URL without parameters was resolved correctly, while the URL with parameter starts with "/User" instead of "/Customer".
Questions:
- Is it a normal behavior? If yes, why does routing work that way?
Is there any workaround for this? I mean is there any way to get the final result as:
<a href="/Customer/History">History</a> <a href="/Customer/History?someParam=qqq">History with param</a>