Asp.net basics-httpmodule

Source: Internet
Author: User

HttpModule provides module initialization and event handling for implementation classes. When an HTTP request arrives at HttpModule, the entire ASP. the. NET Framework system has not processed this HTTP request. That is to say, HttpModule is the "Only Way" for an HTTP request ", therefore, some required information can be appended to the HTTP request information before the HTTP request is passed to the real request processing center (HttpHandler, you can also perform some additional work on the intercepted HTTP request information, or simply terminate the HTTP request that meets certain conditions in some cases, so as to act as a Filter.

1, HTTP request processing process of asp.net

(1 ),The ghost process is intercepted and then transferred to the aspnet_isapi.dll process. Then, it is transmitted to the aspnet_wp.exe process through the Http pipelinepipeline. Then it is sent to the HttpRunTime processing center of the. net framework and sent to the user's browser after processing.
(2 ),After an http request is sent to HttpRuntime, the request will be sent to a container called HttpApplication Factory, the container will provide an HttpApplication instance to process the passed http requests, and then the Http requests will go to the following containers in sequence: HttpModule --> HttpHandler Factory --> HttpHandler. After the HttpHandler's ProcessRequest method in the system is processed, the entire Http Request is processed and the client obtains the corresponding information.
(3)Complete http request processing process in asp.net framework:
HttpRequest --> inetinfo.exe-> Pipeline --> Http Pipeline --> ASPNET_WP.EXE --> HttpRuntime --> HttpApplication Factory --> HttpApplication --> HttpModule --> HttpHandler Factory --> HttpHandler. processRequest ()

That is to say, an HTTP request will be transmitted to the HttpHandler container at a certain time point (ResolveRequestCache event) during the HTTP module container transfer process. After this event, the HttpModule container will create an HttpHandler entry instance, but does not hand over the HTTP Request control at this time, but will continue to trigger the AcquireRequestState event and PreRequestHandlerExcute event. After the PreRequestHandlerExcute event, the HttpModule window will temporarily hand over control to the HttpHandler container for real HTTP request processing.

In the HttpHandler container, the ProcessRequest method is executed to process HTTP requests. After the container HttpHandler processes the entire HTTP request, the control is returned to the HttpModule. The HttpModule continues to perform layer-by-layer forwarding actions on the processed HTTP request information stream until it is returned to the client.

(4)If you want to intercept an httpRequest in the middle and perform some processing on your own, you should implement this internally during the HttpRuntime runtime, specifically in the HttpModule container.

 

2, How HttpModule works

Monitors HttpRequest and adds or filters out some content for HttpRequest.That is, when an HTTP request arrives at HttpModule, the entire ASP. the. NET Framework system has not processed this HTTP request. That is to say, HttpModule is the "Only Way" for an HTTP request ", therefore, some required information can be appended to the HTTP request information before the HTTP request is passed to the real request processing center (HttpHandler, you can also perform some additional work on the intercepted HTTP request information, or simply terminate the HTTP request that meets certain conditions in some cases, so as to act as a Filter.
HttpModule implements the IHttpModule interface. We can customize the classes that implement this interface to replace HttpModule.

3, Compile your own HttpModule

To implement HttpModule, you must implement the IHttpModule interface. The following is an analysis of the IHttpModule interface:

Using System; namespace System. web {public interface IHttpModule {// destroy a resource void Dispose () that is no longer used by HttpModule; // initialize a Module to prepare void Init (HttpApplication context) for capturing HttpRequest );}}

The following is your HttpModule:

1. when the first resource of the site is accessed, Asp. net will create an HttpApplication class instance, which represents the site application, at the same time will create all in the Web. the Module instance that has been registered in Config.
2. Call the Init () method of Modul when creating a Module instance.
3. register the events exposed by HttpApplication that you want to respond to in the Init () method. (Only simple registration of methods is required. The actual method needs to be written separately ).
4. HttpApplication triggers various events in its application cycle.
5. When an event is triggered, call the method that the Module has registered in its Init () method.

Using system; using system. collections. generic; using system. LINQ; using system. web; namespace learning {public class moduledemo: ihttpmodule {// The init method is only used to give the expected event registration method public void Init (httpapplication context) {context. endrequest + = new eventhandler (context_endrequest); context. beginrequest + = new eventhandler (context_beginrequest);} // The actual code for processing the beginrequest event void context_beginrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; httpcontext context = application. context; context. response. write ("<H1 style = 'color: # 00f'> processing from httpmodule, request arrival 

Web. config

<configuration>  <system.web>    <compilation debug="true" targetFramework="4.0" />    

 

In our own http module, assume that the class is named ModuleDemo, which is located in the Learning namespace and the Assembly name is Learning. We only need to copy the Learning to the Bin directory, and in the web. config file system. create an httpModules node under a web node:

Running result:

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.