Asp. NET routing system implementation principle: HttpHandler dynamic mapping

Source: Internet
Author: User
Tags bool

We know that a request is eventually processed through a specific HttpHandler, and the page object that we are familiar with to represent a Web page is a HttpHandler that is used to process requests based on an. aspx file. We can realize the separation between the request address and the physical file path through the HttpHandler dynamic mapping. In fact, asp.net routing system is the implementation of this principle. As shown in the following illustration, ASP. NET routing system intercepts all requests through a custom httpmodule that is registered to the current application, and dynamically matches a httphandler for processing it by analyzing the request. HttpHandler writes the corresponding result to the HTTP reply after processing the request to implement the corresponding request.

The HttpModule type shown in the above illustration as a request interceptor is urlroutingmodule. As shown in the following code snippet, UrlRoutingModule is implemented by registering the HttpApplication Postresolverequestcache event that represents the current application.

   1:public class Urlroutingmodule:ihttpmodule
2: {
3: //other Members
4: Public routecollection routecollection {get; set;}
5: Public void Init (HttpApplication context)
6: {
7: Context . Postresolverequestcache + = new EventHandler (this. Onapplicationpostresolverequestcache);
8:
9: }
the private void Onapplicationpostresolverequestcache (object sender, EventArgs e);
11:}

The UrlRoutingModule has a routecollection property of type RouteCollection, which, by default, refers to the global routing table represented by the RouteTable static property routes. The dynamic mapping of the requested HttpHandler is implemented in the Onapplicationpostresolverequestcache method, and the specific implementation logic is very simple: the HTTP context obtained by HttpApplication, The RouteCollection Getroutedata method is invoked as a parameter to get a Routedata object.

The Routehandler attribute of Routedata can obtain a routing processor object that implements Iroutehandler, and the Gethttphandler method of invoking the latter can get the corresponding HttpHandler object directly. And we need to map to the current request is such a httphandler. The following code fragment basically embodies the dynamic HttpHandler mapping logic defined in the UrlRoutingModule Onapplicationpostresolverequestcache method.

   1:public class Urlroutingmodule:ihttpmodule
2: {
3: //other Members
4: private void Onapplicationpostresolverequestcache (object sender, EventArgs e)
5: {
6: HttpContext context = ((HttpApplication) sender). context;
7: httpcontextbase contextwrapper = new Httpcontextwrapper (context);
8: Routedata routedata = this. Routecollection.getroutedata (Contextwrapper);
9: RequestContext requestcontext = new RequestContext (Contextwrapper, routedata);
Ten: IHttpHandler handler = RouteData.RouteHandler.GetHttpHandler (RequestContext);
One: Context . Remaphandler (handler);
: }
13:}

Second, Pageroutehandler V.s. Mvcroutehandler

We know from the previous introduction that the Routedata object that was obtained for the getroutedata that invoked RouteCollection is routehandler from the matching route object. For route registered by invoking the Mappageroute method of RouteCollection, its routehandler is a type Pageroutehandler object.

Since the purpose of invoking the Mappageroute method is to implement a mapping between the request address and an. aspx paging file, the page object that we are ultimately creating also processes the corresponding request, so the Pageroutehandler Gethttphandler method ultimately returns to the map page The Page object for the path of the face file. In addition, the Mappageroute method can control whether authorization is granted to the physical file address, which is authorized before the Page object is returned.

The HttpHandler acquisition logic defined in Pageroutehandler is basically embodied in the following code fragment, Two properties virtualpath and checkphysicalurlaccess represent the address of the paging file and the need to implement URL authorization for the physical file address, which is initialized in the constructor. And ultimately comes from the arguments passed in by the Mappageroute method that invokes RouteCollection.

 1:public class Pageroutehandler:iroutehandler 
2: {
3:public bool checkphysicalurlaccess {get ; Private set;
4:public string VirtualPath {get; private set;}
5:public Pageroutehandler (string virtualpath, bool checkphysicalurlaccess)
6: {
7:thi S.virtualpath = virtualpath;
8:this. checkphysicalurlaccess = checkphysicalurlaccess;
9:}
10:public IHttpHandler Gethttphandler (requestcontext requestcontext)
One: {
12:if (this.c heckphysicalurlaccess)
: {
://check physical Url Access
:
16: Return (IHttpHandler) Buildmanager.createinstancefromvirtualpath (this. VirtualPath, typeof (Page))
:}

The route object of ASP.net MVC is registered by invoking the RouteCollection extension method Maproute method, which corresponds to a Routehandler object of type Mvcroutehandler. As shown in the following code snippet, Mvcroutehandler is used to get the HttpHandler that handles the current request as a Mvchandler object. Mvchandler implements the activation of controller, the execution of action methods, and the corresponding request, and it is no exaggeration to say that the entire MVC framework is implemented in Mvchandler.

   1:public class Mvcroutehandler:iroutehandler
2: {
3: //other Members
4: Public IHttpHandler Gethttphandler (RequestContext requestcontext)
5: {
6: Return new Mvchandler (RequestContext)
7: }
8:}

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.