In my route config class, I created a custom routing configuration with a static
prefix,
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("MyRoute", "{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
}
But the URL ...mysite/Public
gives a page not found
error. What is wrong here?
Change the order of two routes,
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute("MyRoute", "{controller}/{action}",
new { controller = "Home", action = "Index" });
}
MVC routing goes through the matching patterns according to the given order.
It's trying to generate a URL as Public/index
which is not found.
MVC route system attempts to match an incoming URL against the URL pattern of the route that was defined first and proceeds to the next route only if there is no match. The routes are tried in sequence until a match is found or the set of routes has been exhausted. The result of this is that we must define out most specific routes first.