Asp.Net MVC routing match
First, on the {controller} / {action}
(1), is essential: a MVC system is practical, {controller} / {action} is essential, because it will not be found if there is no error path
(2), the agreed rule: This placeholder is agreed MVC inside, and the controller will automatically parse and a corresponding method of operation
(3) Flexibility: the two conventions may be a placeholder for any position
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//默认路由
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
//含有常量的路由
routes.MapRoute(
name: "test0",
url: "{first}/{second}/{third}/{forth}/aspx",
defaults: new { controller = "Work", action = "Index", id = UrlParameter.Optional }
);
//只有一个控制器的路由
routes.MapRoute(
name: "test1",
url: "{year}/{month}/{day}/{controller}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
//只有一个动作方法的路由
routes.MapRoute(
name: "test2",
url: "{action}/{article}/{author}/{year}/{month}/{day}",
defaults: new { controller = "Work", action = "Index", id = UrlParameter.Optional }
);
}
}
Note: If the route may have been wrong before it appeared certain routes interception, you can change the location or change the routing problem
Second, other placeholder
(1), occupying only: for example, {aa} / {bb} / {cc}, {aa} could not be resolved to the controller {bb} can not be resolved to the operation method
(2), the default requirements: If no one routing {controller} / {action}, or just one of which is predetermined, there is no part of the predetermined default value is used. E.g
url: "{year}/{month}/{day}/{controller}",
defaults: new { controller = "Work", action = "Index", id = UrlParameter.Optional }
This route is matched to a default Url controller Work Index Method
Third, the matching order
(1), preferably used: a plurality of matching routing Url, use is preferentially matched
(2) avoid: defining a plurality of routes, to avoid multiple matches