Routing of ASP. NET source code analysis

Source: Internet
Author: User
In the current version (RC1), form routing in ASP. net mvc (MVC) actually uses system. Web. Routing (. NET SP1 ).
For a separate explanation of system. Web. Routing, see "use ASP. NET web form routing" in the http://msdn.microsoft.com/zh-cn/magazine/2009.01.extremeaspnet.aspx ". Here, we mainly use ASP. net mvc for analysis and analysis.

MVC routing and previously we in ASP. net url rewriting (http://msdn.microsoft.com/zh-cn/library/ms972974.aspx) is similar to the implementation of the HTTP module. For the difference between the two, see: Using ASP. NET web form routing (http://msdn.microsoft.com/zh-cn/magazine/2009.01.extremeaspnet.aspx ).

Core code: urlroutingmodule of the system. Web. Routing namespace and related classes.

Protected virtual void Init (httpapplication Application)
{
// After the beginreques, authenticaterequest, authorizerequestcache, and resolverequestcache events occur
// The postresolverequestcache event is the previous event that has not set an HTTP handler for httpcontent.
Application. postresolverequestcache + = new eventhandler (this. onapplicationpostresolverequestcache );
// When the postmaprequesthandler event occurs, an HTTP handler is set for httpcontent. Therefore, it is best to set a custom HTTP handler for this event.
Application. postmaprequesthandler + = new eventhandler (this. onapplicationpostmaprequesthandler );
}

Private void onapplicationpostmaprequesthandler (Object sender, eventargs E)
{
// Encapsulate the current httpcontent into an httpcontextwrapper object and assign it to the parent class httpcontextbase of httpcontextwrapper
Httpcontextbase context = new system. Web. httpcontextwrapper (httpapplication) sender). context );
This. postmaprequesthandler (context );
}

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

Public Virtual void postmaprequesthandler (httpcontextbase context)
{
// If route information is set in the postresolverequestcache event processing method, rewrite the URL Based on the configuration in the route and set the HTTP handler as the HTTP handler in the route configuration.
// In MVC, the HTTP handler is set to an mvchandler object (Note: The requestcontext attribute of the mvchandler object has been assigned to the system. Web. Routing. requestcontext object)
Requestdata DATA = (requestdata) Context. items [_ requestdatakey];
If (Data! = NULL)
{
Context. rewritepath (data. originalpath );
Context. Handler = data. httphandler;
}
}

Public Virtual void postresolverequestcache (httpcontextbase context)
{
// This. routecollection is a public attribute of urlroutingmodule, and its return type is system. Web. Routing. routecollection.
// This. in the get method of the routecollection attribute, a single-piece routecollection is returned through the static attribute routes of the routetable class (although it seems that routetable does not use the single-piece mode, because the route table is global, it is still a single-piece .)
// Call the getroutedata (httpcontextbase httpcontext) method of the routecollection object. This method traverses itself and searches for routedata that matches the URL. If it does not match, null is returned.
// If you want to know the specific search procedure, you can view the getroutedata method of the routecollection class and the getroutedata method of the route class.
Routedata = This. routecollection. getroutedata (context );
// If the matching route configuration is found
If (routedata! = NULL)
{
// First, check whether the route handler is set in the route configuration (Note: The route handler contains the HTTP handler). In MVC, The routehandler attribute of routedata returns the mvcroutehandler object.
Iroutehandler routehandler = routedata. routehandler;
If (routehandler = NULL)
{
Throw new invalidoperationexception (string. Format (cultureinfo. currentuiculture, routingresources. urlroutingmodule_noroutehandler, new object [0]);
}
// Stoproutinghandler class prevents the route from processing specific requests
If (! (Routehandler is stoproutinghandler ))
{
// The requestcontext class encapsulates an httpcontextbase object and a routedata object.
Requestcontext = new requestcontext (context, routedata );
// Here, the HTTP handler is obtained through the gethttphandler method of the route handler. In MVC, The gethttphandler method of iroutehandler returns the mvchandler object and passes the requestcontext as a parameter to this object.
Ihttphandler httphandler = routehandler. gethttphandler (requestcontext );
If (httphandler = NULL)
{
Throw new invalidoperationexception (string. Format (cultureinfo. currentuiculture, routingresources. urlroutingmodule_nohttphandler, new object [] {routehandler. GetType ()}));
}
// Instantiate a requestdata object to save the Request Path and HTTP handler, and store it in the item set of the current httpcontent
Context. items [_ requestdatakey] = new requestdata {originalpath = context. Request. Path, httphandler = httphandler };
// MVC adds the following configuration to IIS 7.0 in Web. config:
// <Add name = "urlroutinghandler" precondition = "integratedmode" verb = "*" Path = "urlrouting. axd "type =" system. web. httpforbiddenhandler, system. web, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a "/>
// Transfer: this is a simple solution to enable the routing engine to perform routing in IIS 7.0. In fact, the urlrouting module will rewrite the input URL ~ /Urlrouting. axd, which overwrites the URL back to the original input URL.
Context. rewritepath ("~ /Urlrouting. axd ");
}
}
}
Public routecollection
{
Get
{
If (this. _ routecollection = NULL)
{
This. _ routecollection = routetable. routes;
}
Return this. _ routecollection;
}
Set
{
This. _ routecollection = value;
}
}
Private class requestdata
{
[Compilergenerated]
Private ihttphandler _ httphandler;
[Compilergenerated]
Private string _ originalpath;

Public ihttphandler httphandler
{
[Compilergenerated]
Get
{
Return this. _ httphandler;
}
[Compilergenerated]
Set
{
This. _ httphandler = value;
}
}

Public String originalpath
{
[Compilergenerated]
Get
{
Return this. _ originalpath;
}
[Compilergenerated]
Set
{
This. _ originalpath = value;
}
}
}



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.