By default, if the URL can correspond to the entity file under the root directory of the website, it will not be processed through MVC, but will be handed over to ASP. NET for execution.
If routetable. routes. routeexistingfiles = true is set, all files are compared using routing rules,
If the comparison succeeds, it is processed using MVC. If the comparison fails, it is returned to ASP. NET for processing.
1. Add the URL route to the httpapplication processing pipeline
You have registered the urlrouting module in Web. config in C: \ WINDOWS \ Microsoft. NET \ framework \ v4.0.30319 \ config.
<Httpmodules>
<Add name = "UrlRoutingModule-4.0" type = "system. Web. Routing. urlroutingmodule"/>
</Httpmodules>
Ii. Route check
If a matched route exists, find iroutehandler and use the gethttphandler method of routehandler to obtain ihttphandler.
UrlroutingmoduleCodeAs follows:
Protected virtual void Init (httpapplication Application)
{
Application. postresolverequestcache + = new eventhandler (this. onapplicationpostresolverequestcache );
}
Public Virtual void postresolverequestcache (httpcontextbase context)
{
Routedata = This. routecollection. getroutedata (context );
If (routedata! = NULL)
{
Iroutehandler routehandler = routedata. routehandler;
If (routehandler = NULL)
{
Throw new invalidoperationexception (string. Format (cultureinfo. currentuiculture, routingresources. urlroutingmodule_noroutehandler, new object [0]);
}
If (! (Routehandler is stoproutinghandler ))
{
Requestcontext = new requestcontext (context, routedata );
Ihttphandler httphandler = routehandler. gethttphandler (requestcontext );
If (httphandler = NULL)
{
Throw new invalidoperationexception (string. Format (cultureinfo. currentuiculture, routingresources. urlroutingmodule_nohttphandler, new object [] {routehandler. GetType ()}));
}
Context. items [_ requestdatakey] = new requestdata {originalpath = context. Request. Path, httphandler = httphandler };
Context. rewritepath ("~ /Urlrouting. axd ");
}
}
}
3. Default route
In global. asax. CS
Public static void registerroutes (routecollection routes)
{
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");
Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional} // parameter defaults
);
}
{Resource}, {* pathinfo} is a placeholder
4. Add constraints to the route
Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional },
New {id = @ "\ D + "}
);
New {id = @ "\ D +"} the parameter value of the specified ID must be a number.
5. How to generate a URL that can be identified by MVC through URL Routing
@ Routetable. routes. getvirtualpath (request. requestcontext, new routevaluedictionary (New {page = 1}). virtualpath
Displayed on the page:/home/index/2? Page = 1
Add a new routing rule
Routes. maproute (
"Defaultpage", // route name
"{Controller}/{action}/{ID}/{page }"
);
@ Routetable. routes. getvirtualpath (request. requestcontext, new routevaluedictionary (New {page = 1}). virtualpath
Displayed on the page:/home/INDEX/2/1