It has been a long time to learn how to use Asp.net. Now let's analyze the whole process of MVC. I plan to write a blog post in the MVC series, only fromSource codeMVC. When we are exposed to MVC, we will certainly experience routing. How did we develop routing. In our web. config, we have the following sentence: <Add Assembly = "system. Web. Routing, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/> It seems that the route is responsible for it. There is a special class in this DLLUrlroutingmodule
Let's take a look at the main core of it.CodeRight:
Protected virtual void Init (httpapplication application) {If (application. context. items [_ contextkey] = NULL) {application. context. items [_ contextkey] = _ contextkey; application. postresolverequestcache + = new eventhandler (this. onapplicationpostresolverequestcache);} private void onapplicationpostresolverequestcache (Object sender, eventargs e) {httpcontextbase context = new httpcontextwrapper (( (Httpapplication) sender ). context); this. postresolverequestcache (context);} 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, Sr. getstring ("urlroutingmodule_noroutehandler"), new object [0]);} If (! (Routehandler is stoproutinghandler) {requestcontext = new requestcontext (context, routedata); context. request. requestcontext = requestcontext; ihttphandler httphandler = routehandler. gethttphandler (requestcontext); If (httphandler = NULL) {Throw new invalidoperationexception (string. format (cultureinfo. currentuiculture, Sr. getstring ("urlroutingmodule_nohttphandler"), new object [] {Routehandler. GetType ()});} If (httphandler is urlauthfailurehandler) {If (! Formsauthenticationmodule. formsauthrequired) {Throw new httpexception (0x191, Sr. getstring ("assess_denied_description3");} urlauthorizationmodule. reporturlauthorizationfailure (httpcontext. current, this);} else {context. remaphandler (httphandler );}}}}
A postresolverequestcache event is registered in ihttpmodule. init, which calls the postresolverequestcache method. In this method, there are several important codes:
Routedata = This. routecollection. getroutedata (context );
Iroutehandler routehandler = routedata. routehandler;
Requestcontext = new requestcontext (context, routedata );
Context. Request. requestcontext = requestcontext;
Ihttphandler httphandler = routehandler. gethttphandler (requestcontext );
Context. remaphandler (httphandler );
Let's analyze the first routedata = This. routecollection. getroutedata (context). We guess this is to get the route information. To understand this code, you have to go back to us.ProgramIn the registerroutes method in the global. asax. CS file, the default value is
Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional} // default value of the Parameter
);
This code is mainly used to register a route. Note that you cannot write the URL here.Controller and action. How is it implemented?
Public static route maproute (this routecollection routes, string name, string URL, object defaults, object constraints, string [] namespaces) {route = new route (URL, new mvcroutehandler ()) {defaults = new routevaluedictionary (defaults), constraints = new routevaluedictionary (constraints), datatokens = new routevaluedictionary ()}; If (namespaces! = NULL) & (namespaces. length> 0) {route. datatokens ["namespaces"] = namespaces;} routes. Add (name, route); Return route ;}
The parameters are as follows:
Routename = "default", // route name
Routeurl ="{Controller}/{action}/{ID} ", // URL with Parameters
Defaults = new {controller = "home", Action = "Index", id = urlparameter. optional} // default value of the Parameter
Constraints = NULL
Namespaces = NULL
A route instance is created and added to the routecollection.
If there are special needs in our project, do you need to create your own httphandler?
In fact, it's easy to register our iroutehandler,Routes. Add (new route ("{controller}/{action}/{ID}", new mvcroutehandler (); then, gethttphandler implements its own logic processing.
Now let's go back to the routedata = This. routecollection. getroutedata (context); Code. The main code for getroutedata is as follows:
Public routedata getroutedata (httpcontextbase httpcontext)
{
Using (this. getreadlock ())
{
Foreach (routebase base2 in this)
{
Routedata = base2.getroutedata (httpcontext );
If (routedata! = NULL)
{
Return routedata;
}
}
}
Return NULL;
}
Here, base2 is the previously added route for calling maproute. The getroutedata method of route is as follows:
Public override routedata getroutedata (httpcontextbase httpcontext) {string virtualpath = httpcontext. request. apprelativecurrentexecutionfilepath. substring (2) + httpcontext. request. pathinfo; routevaluedictionary values = This. _ parsedroute. match (virtualpath, this. defaults); If (values = NULL) {return NULL;} routedata DATA = new routedata (this, This. routehandler); If (! This. processconstraints (httpcontext, values, routedirection. incomingrequest) {return NULL;} foreach (keyvaluepair
pair in values) {data. values. add (pair. key, pair. value);} If (this. datatokens! = NULL) {foreach (keyvaluepair
pair2 in this. datatokens) {data. datatokens [pair2.key] = pair2.value ;}} return data ;}
This method is very complicated and involves many verification and checks.Routedata DATA = new routedata (this, this. routehandler);
Of course, the restRequestcontext = new requestcontext (context, routedata );
Context. Request. requestcontext = requestcontext;There is nothing special about the two sentences.
Now let's take a look.Ihttphandler httphandler = routehandler. gethttphandler (requestcontext );What does this sentence do? It means to get httphandler.
Then how does mvcroutehandler obtain an httphandler,
Protected virtual ihttphandler gethttphandler (requestcontext ){
Requestcontext. httpcontext. setsessionstatebehavior (getsessionstatebehavior (requestcontext ));
Return new mvchandler (requestcontext );
}
An mvchandler instance is directly returned.
The mostContext. remaphandler (httphandler );It's very easy to understand. There is such a sentence in the remaphandler method of httpcontext.This. _ remaphandler = Handler;
This attribute is available in httpcontext.
Internal ihttphandler remaphandlerinstance
{
Get
{
Return this ._Remaphandler;
}
}
So when will this item be called? The Void httpapplication. iexecutionstep. Execute () method in the internal class materializehandlerexecutionstep of httpapplication is called.
If (httpcontext. remaphandlerinstance! = NULL)
{
Httpcontext. Handler = httpcontext. remaphandlerinstance;
}
I can guess the class name materializehandlerexecutionstep. The buildsteps methods in pipelinestepmanager of internal classes include:
Httpapplication. iexecutionstep step = new httpapplication. materializehandlerexecutionstep (APP );
App. addeventmapping ("managedpipelinehandler", requestnotification. maprequesthandler, false, step );
I think you should have a general understanding of the entire MVC route here.