URL attribute routing: attribute routing can customize the routing rules in the Controller and action. The disadvantage is that it is not a good unified management URL
To register an attribute route:
1 Public Static voidregisterroutes (routecollection routes)2 {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 5Routes. Mapmvcattributeroutes ();//registering feature Routes6 7 routes. MapRoute (8Name"Default",9Url:"{Controller}/{action}/{id}",TenDefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional} One ); A } - //Use the routing attribute on the controller to omit the controller inside the action -[Routeprefix ("Home")] the Public classHomecontroller:controller - { - Publicactionresult Index () - { + returnView (); - } + //Attribute Routing A //URLhttp://localhost: 17749/HOME/ORDER/2 at //page data Format validation -[Route ("Home/order/{page:int}")] - PublicActionResult OrderList (intpage) - { -Viewbag.pageindex =page; - returnView (); in}
2. Traditional route traditional routes can be centrally configured routing rules, procedures for later maintenance is more convenient
1 Public Static voidregisterroutes (routecollection routes)2 {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 5Routes. Mapmvcattributeroutes ();//registering feature Routes6 //the custom name must precede the default route7 routes. MapRoute (8 "Order",9 "Order/{id}",Ten New{controller ="Order", action ="Details", id =urlparameter.optional}, One New{id =@"\d" } A ); - - routes. MapRoute ( theName"Default", -Url:"{Controller}/{action}/{id}", -DefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional} - ); + - } + A at -[Routeprefix ("Order")] - Public classOrdercontroller:controller - { - //Attribute Routing -[Route ("")] in[Route ("list/{page:int?}")] - PublicActionResult Index (intpage =1) to { +Viewbag.pageindex =page; - returnView (); the } * //Legacy Routing $ PublicActionResult Details (intID)Panax Notoginseng { -Viewbag.id =ID; the returnView (); + } A}
<a href= "@Url. ROUTEURL (" Order ", new {id = 1})" target= "_blank" >order Details 1</a>
<a href= "@Url. ROUTEURL (" Order ", new {id = 2})" target= "_blank" >order Details 2</a>
MVC Routing Uses