The HTTP request processing process for ASP.

Source: Internet
Author: User
Tags httpcontext

1. The HTTP request processing process of ASP.

Description
(1), the client browser sends an HTTP request to the server, the request is intercepted by the Inetinfo.exe process, then forwarded to the Aspnet_isapi.dll process, and then it is routed through the HTTP pipeline pipeline to Aspnet_ Wp.exe this process, the next step is to the. NET Framework's httpruntime processing center, which is sent to the user's browser after processing is complete.
(2), when an HTTP request is fed into httpruntime, the HTTP request continues to be fed into a container called HttpApplication factory, This container will give a HttpApplication instance to handle the incoming HTTP request, and the HTTP request will then go into the following containers: HttpModule---HttpHandler Factory-- HttpHandler. When the ProcessRequest method of HttpHandler inside the system is finished, the entire HTTP request is processed and the client gets the corresponding stuff.
(3) The process flow of the complete HTTP request in the ASP. NET Framework:
Httprequest-->inetinfo.exe->aspnet_isapi. Dll-->http pipeline-->aspnet_wp. Exe-->httpruntime-->httpapplication Factory-->httpapplication-->httpmodule-->httphandler Factory-->httphandler-->httphandler.processrequest ()

This means that an HTTP request is passed to the HttpHandler container at some point (Resolverequestcache event) during the delivery of the HttpModule container. After this event, the HttpModule container creates a HttpHandler instance of the portal, but does not hand over the HTTP request control at this time. Instead, it continues to trigger the AcquireRequestState event and the Prerequesthandlerexcute event. After the Prerequesthandlerexcute event, the HttpModule window temporarily gives control to the HttpHandler container for real HTTP request processing.
The ProcessRequest method is executed inside the HttpHandler container to handle HTTP requests. After the container HttpHandler finishes processing the entire HTTP request, the control is returned to Httpmodule,httpmodule, which continues the layer-by-layer handoff of the processed HTTP request flow until it returns to the client.

(4)If you want to intercept a httprequest in the middle and do some of your own processing, it should be in the HttpRuntime runtime to do this, in fact, in HttpModule this container to achieve.
2. HttpModule Working principle


be responsible for monitoring httprequest, while adding or filtering out part of HttpRequest. that is, when an HTTP request arrives at HttpModule, the entire ASP. NET Framework system does not do any processing of this HTTP request, which means that for HTTP requests, HttpModule is an HTTP request " , so you can append some required information to this HTTP request message before the HTTP request is passed to the Real Request Processing Center (HttpHandler), or do some extra work on the intercepted HTTP request message, Or, in some cases, simply terminating an HTTP request that satisfies some condition, which can act as a filter.
HttpModule implements the interface IHttpModule, we can customize the class that implements the interface, thus replacing the HttpModule.
The default httpmodule for ASP. NET is as follows:

        1.system.web.sessionstate.sessionstatemodule;
        2.system.web.security.windowsauthenticationmodule;
        3.system.web.security.formsauthenticationmodule;
        4.system.web.security.passportauthenticationmodule;
        5.system.web.security.urlauthorizationmodule;
        6.system.web.security.fileauthorizationmodule;
3. Write your own httpmodule.

To implement HttpModule, interface IHttpModule must be implemented. Here is the IHttpModule interface analysis:

Using System;
Namespace System.Web
{
   public interface IHttpModule
    {
           destroying resources that are no longer used by HttpModule
        void Dispose ();
        Initialize a module to prepare for capturing HttpRequest
        void Init (HttpApplication context);
    }
}
Here's your HttpModule:
Using System;
Using System.Web;
Namespace ClassLibrary1
{
    public class Myhttpmodule:ihttpmodule
    {
        public void Dispose () {}
        public void Init (HttpApplication context)
        {
            Context. BeginRequest + = new EventHandler (application_beginrequest);
            Context. EndRequest + = new EventHandler (application_endrequest);
        }
        public void Application_BeginRequest (object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = Application. Context;
            HttpResponse response = context. Response;
            Response. Write ("This is from a custom HttpModule with beginrequest");
        }
        public void Application_EndRequest (object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = Application. Context;
            HttpResponse response = context. Response;
            Response. Write ("This is from a custom HttpModule with endrequest");
        }
    }
}
Web. config:
Myhttpmodule "type=" Classlibrary1.myhttpmodule,classlibrary1 "/>

Default.aspx.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
{
    protected void Page_Load (object sender, EventArgs e)
    {
        Response.Write ("From Default.aspx page ");
    }
}
4. HttpModule Internal event mechanism and life cycle

HttpModule HttpApplication instances are processed, and HttpApplication has many events (corresponding to different lifetimes), thus deriving HttpModule internal event mechanism and life cycle.
(1), HttpModule events

BeginRequest Indicates that the request processing started
AuthenticateRequest Encapsulating the request authentication process
AuthorizeRequest Encapsulates the process of checking whether a previously cached output page can be used to process requests
Resolverequestcache Get triggered from the cache at the appropriate time
AcquireRequestState triggered when the initialization session is loaded
PreRequestHandlerExecute Triggered before an HTTP request enters HttpHandler
PostRequestHandlerExecute Triggered after an HTTP request enters HttpHandler
ReleaseRequestState Triggers when the session state is stored
Updaterequestcache Triggers when cache information is updated
EndRequest triggered when HTTP request processing is complete
Presendrequesthenaders Triggered before the header is sent to the client
Presendrequestconternt Triggered before content is sent to the client

Description
A, benginrequest and endrequest are the first and last events of the HttpModule container respectively;
B, EndRequest will also trigger the Presendrequestheaders event and Presendrequestcontent event, which is not the two events outside the HttpModule, which means that the HttpModule ends, Going to start sending data to the client

2), verifying the HttpModule life cycle
Interaction with HttpHandler:
Description
A, HttpModule container will pass HttpRequest to HttpHandler container, this point is Resolverequestcache event
B, HttpModule container will establish HttpHandler instance as entry--session effective
C, triggering acquirerequeststate events and PreRequestHandlerExecute events
D, the HttpModule container will transfer control over the HttpRequest to the HttpHandler container.
E, HttpHandler container processing httprequest--use their own ProcessRequest method, will give their control to HttpModule container-after the session failure.
Verify the Life Cycle Code:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web;
Namespace Myhttpmodule
{
    public class Validaterhttpmoduleevents:ihttpmodule
    {
        public void Dispose ()
        { }
        
        Verifying the HttpModule event mechanism
        
        
        public void Init (HttpApplication application)
        {
            Application. BeginRequest + = new EventHandler (application_beginrequest);
            Application. EndRequest + = new EventHandler (application_endrequest);
            Application. AcquireRequestState + = new EventHandler (application_acquirerequeststate);
            Application. AuthenticateRequest + = new EventHandler (application_authenticaterequest);
            Application. AuthorizeRequest + = new EventHandler (application_authorizerequest);
            Application. PreRequestHandlerExecute + = new EventHandler (Application_prerequesthandlerexecute);
            Application. PostRequestHandlerExecute + = new EventHandler (Application_postrequesthandlerexecute);
            Application. ReleaseRequestState + = new EventHandler (application_releaserequeststate);
            Application. Resolverequestcache + = new EventHandler (Application_resolverequestcache);
            Application. Presendrequestheaders + = new EventHandler (application_presendrequestheaders);
            Application. Presendrequestcontent + = new EventHandler (application_presendrequestcontent);
        }
        private void Application_BeginRequest (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_BeginRequest
");
        }
        private void Application_EndRequest (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_EndRequest
");
        }
        private void Application_prerequesthandlerexecute (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_prerequesthandlerexecute
");
        }
        private void Application_postrequesthandlerexecute (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_postrequesthandlerexecute
");
        }
        private void Application_releaserequeststate (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_releaserequeststate
");
        }
        private void Application_acquirerequeststate (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_acquirerequeststate
");
        }
        private void Application_presendrequestcontent (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_presendrequestcontent
");
        }
        private void Application_presendrequestheaders (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_presendrequestheaders
");
        }
        private void Application_resolverequestcache (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_resolverequestcache
");
        }
        private void Application_authorizerequest (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_authorizerequest
");
        }
        private void Application_AuthenticateRequest (object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication) sender;
            Application. Context.Response.Write ("Application_AuthenticateRequest");
        }
    }
}
HttpModule1 "type=" Myhttpmodule.httpmodule1,myhttpmodule "/>
HttpModule2 "type=" Myhttpmodule.httpmodule2,myhttpmodule "/>

HttpModule1 and HttpModule2 imitate validaterhttpmoduleevents (except that the class name changes, the events and methods do not change), and the code is not pasted.

As you can see from the running results, the order in which custom HttpModule are introduced in the Web. config file determines the order in which multiple custom HttpModule handle the takeover of an HTTP request.

(3) using HttpModule to terminate this HttpRequest request

In the BeginRequest event, the Httpapplication.completerequest () method can be used to terminate this HttpRequest request when certain conditions are met

Using System;
Using System.Web;
Namespace ClassLibrary1
{
    public class Myhttpmodule:ihttpmodule
    {
        public void Dispose () {}
        public void Init (HttpApplication context)
        {
            Context. BeginRequest + = new EventHandler (application_beginrequest);
        }
        public void Application_BeginRequest (object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            Application.completerequest ();
            Application. Context.Response.Write ("request terminated");
        }
    }
}

Description
A, for a httpmodule, terminates in Beginrquest, but still calls the EndRequest event, as well as the Presendrequestheaders event and Presendrequestcontent event. It can also be said to jump directly to the EndRequest event without invoking the event in this period
b, if there are two HttpModule, terminated in the first HttpModule beginrequest, only the beginrequest of the second HttpModule will not be called, but two endrequest events will still be invoked. As well as presendrequestheaders events and Presendrequestcontent events.


The HTTP request processing process for ASP.

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.