1. Routeattribute Overview
The Routeattribute namespace is SYSTEM.WEB.MVC, which differs from the Web API's Routeattribute (its namespace is System.Web.Http)
By default, the feature routing (routeattribute) feature of MVC is turned off and requires a manual start-up mode:
Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. Mapmvcattributeroutes ();//Enable attribute Routingroutes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }
2, Routeattribute Simple example
[Route ("list.html")] [Route ("list_{category}.html")] [Route ("list_{category}_p{page:int}.html")] Public voidArticlelist (stringCategoryintpage =1) { vartitle =string. IsNullOrEmpty (category)?"All News": Category +"News"; Response.Write (string. Format ("Category: {0}, page:{1}<br/> We are all good kids!!! ", title, page)); }
Route ("list.html") match address: http://localhost:2000/list.html
Route ("list/{category}.html") match address: http://localhost:2000/list/news.html
Route ("list/{category}/{page:int}.html") match address: http://localhost:2000/list/bews/2.html
MVC5 new Features (i) routeattribute create your own URL rules