in ASP. NET MVC, a URL request is handled by an action in the corresponding controller, and the URL routing tells MVC how to navigate to the correct controller and action.
default route table
When we create a new ASP. NET MVC program in VS, the program will automatically use the default routing table.
Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default",//default route nameUrl:"{Controller}/{action}/{id}",//URLs and ParametersDefaultsNew{controller ="Home", action ="Index", id = urlparameter.optional}//Default Parameters ); }
The code above is to add a route named (Default) to the routing table.
In the URL
{Controller} corresponds to the director name,
{action} corresponds to the action in the controller,
{ID} corresponds to a parameter,
Defaults is the default controller = "Home" action = "Index".
When you enter/home in the address bar because the action has a corresponding default value, the program executes Homecontroller.index ().
When you enter/HOME/INDEX/6 in the Address bar, the route automatically corresponds to the parameter
Controller = Home
Action = Index
ID = 6
The program will also execute Homecontroller.index (6).
Examples show:
The above static method RegisterRoutes is called in the Application_Start method in the Global.asax.cs file.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;namespacemvcapplication{//note:for instructions on enabling IIS6 or IIS7 Classic mode,//Visithttp://go.microsoft.com/?LinkId=9394801 Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); routeconfig.registerroutes (routetable.routes); } }}
It also contains other methods.
Custom route Table
Add a route table of your own definition below
Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default",//default route nameUrl:"{Controller}/{action}/{id}",//URLs and ParametersDefaultsNew{controller ="Home", action ="Index", id = urlparameter.optional},//Default ParametersConstraintsNew{action =@"[a-za-z]+"}//Match letters only ); Routes. MapRoute (Name:"Myroute",//Route nameUrl:"News/{newsid}",//URL with no parametersDefaultsNew{controller ="News", action ="Index"},//action to index is specified by defaultConstraintsNew{NewSID =@"\d+"}//NewSID must be a digital ); }
When you enter/news/index?newsid=1 and/NEWS/1 in the address bar, the effect is the same.
Access/NEWS/1 It accesses Controller=news Action=index by default and then the parameter is 1.
I add a limit to the default routing rule so that the action only executes letters to avoid overwriting the newly added routing rules.
This way we can shorten the URL address and make the URL more intuitive and friendly.
Routing constraints:
Regular expression constraints
Rules are constrained by regularization.
routes. MapRoute (Name:"Myroute",//Route nameUrl:"News/{newsid}",//URL with no parametersDefaultsNew{controller ="News", action ="Index"},//action to index is specified by defaultConstraintsNew{NewSID =@"\d+"}//NewSID must be a digital);
The constraint NewSID is the number above.
routes. MapRoute (Name:"Default",//default route nameUrl:"{Controller}/{action}/{id}",//URLs and ParametersDefaultsNew{controller ="Home", action ="Index", id = urlparameter.optional},//Default ParametersConstraintsNew{action =@"[a-za-z]+"}//Match letters only);
The above constraint action is only a letter, and no other rules are allowed.
HTTP request mode constraint
routes. MapRoute (Name:"Myroute",//Route nameUrl:"News/{newsid}",//URL with no parametersDefaultsNew{controller ="News", action ="Index"},//action to index is specified by defaultConstraintsNew{NewSID =@"\d+", HttpMethod =NewHttpmethodconstraint ("POST") }//NewSID must be a number and the request must be post);
Above for constrained HTTP request for post
Custom Routing constraints
If the standard routing constraints do not meet your needs, you can define your own routing constraint rules by implementing the Irouteconstraint interface.
The following is a constraint that implements whether a request is from a local.
Public class Localhostconstraint:irouteconstraint { publicbool Match ( httpcontextbase HttpContext, Route route, string parametername, routevaluedictionary values, routedirection routedirection ) { return httpContext.Request.IsLocal; } }
Applications in the routing table:
routes. MapRoute (Name:"Default",//default route nameUrl:"{Controller}/{action}/{id}",//URLs and ParametersDefaultsNew{controller ="Home", action ="Index", id = urlparameter.optional},//Default ParametersConstraintsNew{action =@"[a-za-z]+", isLocal =NewLocalhostconstraint ()}//matches only letters and requests only local);
The above is constrained by the HTTP request from local.
Sample code Download
If you think this article is helpful to you, please click "Recommend", thank you.
ASP. NET MVC Learning Route (URL Routing)