ASP. net mvc 1.0 Process Analysis (system. Web. Routing) [zz]

Source: Internet
Author: User

MVC depends on system. Web. Routing to process Request Path parsing. That is to say, the process starts from system. Web. Routing. urlroutingmodule.

Web. config
<Httpmodules>
<Add name = "urlroutingmodule" type = "system. Web. Routing. urlroutingmodule, system. Web. Routing,..."/>
</Httpmodules>

Then let's take a look at what the urlroutingmodule has done internally.

Public class urlroutingmodule: ihttpmodule
{
Protected virtual void Init (httpapplication Application)
{
Application. postresolverequestcache + = new eventhandler (this. onapplicationpostresolverequestcache );
Application. postmaprequesthandler + = new eventhandler (this. onapplicationpostmaprequesthandler );
}
}

Two httpapplication events are subscribed. In the lifecycle of ASP. NET applications, postresolverequestcache is triggered before postmaprequesthandler, and finally ihttphandler. processrequest () is used to process the final request.

The request context is encapsulated for further processing.

Public class urlroutingmodule: ihttpmodule
{
Private void onapplicationpostmaprequesthandler (Object sender, eventargs E)
{
Httpcontextbase context = new httpcontextwrapper (httpapplication) sender). context );
This. postmaprequesthandler (context );
}

Private void onapplicationpostresolverequestcache (Object sender, eventargs E)
{
Httpcontextbase context = new httpcontextwrapper (httpapplication) sender). context );
This. postresolverequestcache (context );
}
}

First, postresolverequestcache is executed.

Public class urlroutingmodule: ihttpmodule
{
Public Virtual void postresolverequestcache (httpcontextbase context)
{
Routedata = This. routecollection. getroutedata (context );

If (routedata! = NULL)
{
Iroutehandler routehandler = routedata. routehandler;

...

If (! (Routehandler is stoproutinghandler ))
{
Requestcontext = new requestcontext (context, routedata );
Ihttphandler httphandler = routehandler. gethttphandler (requestcontext );

...

Requestdata data2 = new requestdata ();
Data2.originalpath = context. Request. path;
Data2.httphandler = httphandler;
Context. items [_ requestdatakey] = data2;
Context. rewritepath ("~ /Urlrouting. axd ");
}
}
}
}

Routecollection is actually a reference to routetable. routes.

Public class urlroutingmodule: ihttpmodule
{
Public routecollection
{
Get
{
If (this. _ routecollection = NULL)
{
This. _ routecollection = routetable. routes;
}
Return this. _ routecollection;
}
Set
{
This. _ routecollection = value;
}
}
}

Okay, I jumped again.

Public class routetable
{
Private Static routecollection _ instance = new routecollection ();

Public static routecollection routes
{
Get
{
Return _ instance;
}
}
}
At this time, we will mention the code in global. asax. CS.

Public class mvcapplication: system. Web. httpapplication
{
Public static void registerroutes (routecollection routes)
{
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");

Routes. maproute (
"Default ",
"{Controller}/{action}/{ID }",
New {controller = "home", Action = "Index", id = ""}
);

}

Protected void application_start ()
{
Registerroutes (routetable. routes );
}
}

Both ignoreroute and maproute are extensions provided in system. Web. MVC. dll.

Public static class routecollectionextensions
{
Public static void ignoreroute (this routecollection routes, string URL );
...
Public static route maproute (this routecollection routes, string name, string URL );
...
}

The only thing to note is the internal details of the method. The registered ihttphandler is mvcroutehandler.

Public static class routecollectionextensions
{
Public static route maproute (this routecollection routes, string name ,...)
{
Route <> G _ initlocal1 = new route (URL, new mvcroutehandler ());
<> G _ initlocal1.defaults = new routevaluedictionary (defaults );
<> G _ initlocal1.constraints = new routevaluedictionary (constraints );
Route route = <> G _ initlocal1;

If (namespaces! = NULL) & (namespaces. length> 0 ))
{
Route. datatokens = new routevaluedictionary ();
Route. datatokens ["namespaces"] = namespaces;
}

Routes. Add (name, route );
Return route;
}

Public static void ignoreroute (this routecollection routes, string URL, object constraints)
{
Ignorerouteinternal <> G _ initlocal0 = new ignorerouteinternal (URL );
<> G _ initlocal0.constraints = new routevaluedictionary (constraints );
Ignorerouteinternal route = <> G _ initlocal0;
Routes. Add (route );
}

Private sealed class ignorerouteinternal: Route
{
Public ignorerouteinternal (string URL): Base (URL, new stoproutinghandler ()){}
}
}

Public class mvcroutehandler: iroutehandler
{
Protected virtual ihttphandler gethttphandler (requestcontext );
Ihttphandler iroutehandler. gethttphandler (requestcontext );
}

All right, go back to urlroutingmodule. postresolverequestcache.

Public class urlroutingmodule: ihttpmodule
{
Public Virtual void postresolverequestcache (httpcontextbase context)
{
Routedata = This. routecollection. getroutedata (context );

If (routedata! = NULL)
{
Iroutehandler routehandler = routedata. routehandler;

...

If (! (Routehandler is stoproutinghandler ))
{
Requestcontext = new requestcontext (context, routedata );
Ihttphandler httphandler = routehandler. gethttphandler (requestcontext );

...

Requestdata data2 = new requestdata ();
Data2.originalpath = context. Request. path;
Data2.httphandler = httphandler;

Context. items [_ requestdatakey] = data2;
Context. rewritepath ("~ /Urlrouting. axd ");
}
}
}
}
After obtaining routedata and mvcroutehandler, call mvcroutehandler. gethttphandler () to obtain ihttphandler.

Public class mvcroutehandler: iroutehandler
{
Protected virtual ihttphandler gethttphandler (requestcontext)
{
Return new mvchandler (requestcontext );
}

Ihttphandler iroutehandler. gethttphandler (requestcontext)
{
Return this. gethttphandler (requestcontext );
}
}

Good! The emergence of mvchandler means that the execution process has been transferred from system. Web. Routing to system. Web. MVC. Urlroutingmodule. postresolverequestcache () Finally saves the relevant request parameters to the context. There is also a strange line of code to see how the source code we downloaded from Microsoft symbol server will explain.

Procedure:

1. First add a postresolverequestcache breakpoint (we can set a breakpoint for methods without source code ).

2. Download the symbol file.


3. Open the source code.

Okay. Let's see what the original author said.

Public Virtual void postresolverequestcache (httpcontextbase context)
{
// Save data to be used later in Pipeline
Context. items [_ requestdatakey] = new requestdata ()
{
Originalpath = context. Request. path,
Httphandler = httphandler
};

// Rewrite path to something registered as a managed handler in IIS. This is necessary so iis7 will
// Execute our managed handler (instead of say the static file handler ).
Context. rewritepath ("~ /Urlrouting. axd ");
}
Let's continue with the process. According to the ASP. NET httpapplication event sequence, then urlroutingmodule. postmaprequesthandler () will be executed.

Public class urlroutingmodule: ihttpmodule
{
Public Virtual void postmaprequesthandler (httpcontextbase context)
{
Requestdata DATA = (requestdata) Context. items [_ requestdatakey];
If (Data! = NULL)
{
Context. rewritepath (data. originalpath );
Context. Handler = data. httphandler;
}
}
}

Requestdata is obviously the data stored in postresolverequestcache. By setting httpcontext. Handler to mvchandler, subsequent execution can be performed. By the way, the original author also describes the rewritepath of this method.

Public Virtual void postmaprequesthandler (httpcontextbase context)
{
Requestdata = (requestdata) Context. items [_ requestdatakey];

If (requestdata! = NULL)
{
// Rewrite the path back to its original value, so the request handler only sees the original path.
Context. rewritepath (requestdata. originalpath );

// Set context. Handler to the ihttphandler determined earlier in the pipeline.
Context. Handler = requestdata. httphandler;
}
}

All right, this is the Process Analysis for system. Web. routing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.