Overview of ASP. _httpmodule and Application life cycle _1

Source: Internet
Author: User
Tags httpcontext

Why is ASP. NET behind, still talking about these? Because MVC is still in the old framework, a request module is added, triggering in the seventh event;

First, the concept

An HTTP module (HttpModule) is an assembly that is called each time a request is made to an application by implementing the IHttpModule interface and handling events. http Module as ASP. NET request pipeline to access life-cycle events throughout the request process. Therefore, theHTTP module gives us the opportunity to check incoming and outgoing requests and take action based on that request , and even we can participate in the ASP by implementing an HTTP module . NET Operation Management.   

ii. Managed pipeline mode (IIS application pool)

1. Classic mode

before IIS7, ASP. NET is added to IIS in the same way as IIS ISAPI extension. IIS then makes judgments based on the required content type, and if it is an HTML static Web page, it is handled by IIS itself, and if not, it is assigned to the respective IIS ISAPI Extension according to the requested content type. If the required content type is ASP, it is assigned to the processing ASP. NET IIS ISAPI Extension, which is aspnet_isapi.dll. The "Classic mode" in the IIS7 application pool managed pipeline mode works as well, and this mode is compatible with IIS 6 to reduce the upgrade cost. This is the architecture:

2. Integrated mode

After IIS7 fully integrated. NET, the processing order of the schemas is very different, with the biggest change being that the IIS plug-in (ISAPI Extension) role has entered the IIS core and can handle many of the IIS7 's type requirements with the ASP. These ASP. NET modules can not only deal with ASP. NET Web-page programs, but also other programs; look at the picture:

3. Life cycle in IIS5, IIS6, processing order

A. The user requests application resources from the Web server.

asp. NET application to web Server (for asp for the browser. NET applications, typically iis) send requests as a starting point. asp. NET is the isapi extension under the web server. web server receives a request, the file name extension of the requested file is checked to determine which isapi extension processes the request and then passes the request to the appropriate isapi extension. asp. NET handles file extensions that have been mapped to them, such as .aspx, .ascx, .ashx, and asmx.

If the file name extension is not already mapped to ASP . NET will not receive the request. For use with ASP. NET authentication applications, it is important to understand this. For example , an. htm file, which is typically not mapped to ASP . NET will not perform authentication or authorization checks on. htm file requests, so even if the file contains only static content, you should use the map to ASP if you want to check for authentication with ASP . NET file name extension to create the file, such as the . aspx file name extension.

If you want to create a custom handler (HttpHandler) that serves a specific file name extension, you must map the extension to ASP. NET in IIS, and you must register the handler in the application's Web. config file.

B.asp. NET receives the first request to an application.

When ASP. NET receives a request for any resource in the application, the class named Applicationmanager creates an application domain. The WWW site runs in it. As we all know, the application domains of two Web applications are independent (isolated) on the same IIS. Therefore, problems in one application domain do not affect other application domains. In the application domain, an instance is created for the class named Hostingenvironment that provides access to information about the application, such as the folder name where the application is stored.

The following diagram illustrates this relationship:

C. Create an ASP. NET core object for each request.

After the application domain is created and the Hostingenvironment object is instantiated,ASP. NET will create and initialize core objects such as HttpContext,HttpRequest, and HttpResponse. the HttpContext class contains objects that are specific to the current application request, such as HttpRequest and HttpResponse objects. the HttpRequest object contains information about the current request, including cookie and browser information. the HttpResponse object contains the response sent to the client, including all rendered output and cookies.

D. Assign the HttpApplication object to the request.

After all of the core application objects are initialized, the application is started by creating an instance of the HttpApplication class. If the application has a Global.asax file, then ASP. NET creates an instance of the Global.asax class (derived from the HttpApplication class) and uses that derived class to represent the application.

The first time an ASP is requested in the application . NET page or process, a new instance of HttpApplication is created. To maximize performance, you can reuse HttpApplication instances for multiple requests .

When you create an instance of HttpApplication, all the configured modules are created at the same time. After all the configured modules have been created, the Init method of the HttpApplication class is called .

The following diagram illustrates this relationship:

E: The request is processed by the HttpApplication pipeline.

The HttpModule event is executed by the HttpApplication class when the request is processed .

4. Life cycle in the IIS7
A. Issue a request for application resources.

Asp. NET application is the starting point for the browser to send requests to the Web server.

In IIS7.0 Classic mode, as well as in IIS6.0,ASP. NET request pipeline is detached from the Web server pipeline, the module applies only to requests that are routed to an ASP. asp extension, and if the file name extension of the requested resource type is not explicitly mapped to ASP . NET features, such as static files, because of ASP. The NET runtime does not process the request.

In IIS7.0 Integrated mode, all requests are processed by a single unified pipeline, and when the integration pipeline receives the request, it goes through some phases that are common to all requests, which are represented by the Requestnotification enumeration, and all requests can be configured to use ASP. NET functionality, which is encapsulated in managed code modules that can access the request pipeline. For example, a request to an HTML page will still call ASP, even if the. htm file name extension is not explicitly mapped to ASP . NET module. This allows us to use ASP for all resources . NET authentication and authorization.

B. The unified pipeline receives the first request to the application.

When the unified pipeline receives the first request for any resource in the application, an instance is created for the Applicationmanager class, which is the application domain that processes the request, and the application domain provides the separation of global variables between applications. and enable each application to be uninstalled separately. In the application domain, an instance is created for the Hostingenvironment class that provides access to information about the application, such as the name of the folder where the application is stored.

C. A response object will be created for each request.

After you create the application domain and instantiate the Hostingenvironment object, the application objects, such as HttpContext,HttpRequest, and HttpResponse, are created and initialized . the HttpContext class contains objects that are specific to the current application request, such as HttpRequest and HttpResponse objects. the HttpRequest object contains information about the current request, including Cookie and browser information. the HttpResponse object contains the response sent to the client, which includes all rendered output and cookies.

D. Assign the HttpApplication object to the request.

After all application objects are initialized, the application is started by creating an instance of the HttpApplication class. If the application has a Global.asax file, then ASP. NET creates an instance of the Global.aspx class derived from the HttpApplication class . The derived class is then used to represent the application.

The first time an ASP is requested in the application . NET page or process, a new instance of the HttpApplication class is created. To maximize performance, you can reuse HttpApplication instances for multiple requests .

Which ASP to load . NET modules depend on the managed code modules that the application inherits from the parent application, and also on which modules are configured in the configuration section of the application's Web. config file. Add or remove modules from the modules element in the system.webserver section of the application's Web. config. I will write a special article to record the commonly used webconfig

F. The request is processed by the HttpApplication pipeline.

When a request is processed, the HttpApplication class executes a series of events, which are also useful if you are developing a custom module and want to invoke the module for all requests destined for the pipeline. The custom module implements the IHttpModule interface, and in IIS7.0 Integrated mode, the event handler must be registered in the module's init method.

Overview of ASP. _httpmodule and Application life cycle _1

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.