Click here to Skip to main content
15,895,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I have been following Pro ASP.NET MVC 4 book by Adam Freeman, and i'm confused about the ActionLink's route matching behaviour. Below are my RegisterRoute and ActionLink method :

C#
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("NewRoute", "App/Do{action}",
new { controller = "Home" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional });
}


@Html.ActionLink("This is an outgoing URL", "CustomVariable")


The ActionLink above points to
<a href="/App/DoCustomVariable">This is an outgoing URL</a>


I find that puzzling. According to the routing rules, it shouldn't match with
/App/DoCustomVariable
since it lacks the static fragment for it. It should point to /Home.

Thanks in advance.
Posted

1 solution

It looks for a match in order and returns the first valid one. So while it matches your second route, it also matches the first so that is the route used. Put your most generic routes at the end and the most specific ones at the start.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900