HttpHandler and HttpModule

Source: Internet
Author: User

ASP. when the Http Request is processed by Net, the requests are processed by each HttpModule in the Pipeline (Pipeline) mode and then to HttpHandler. After the HttpHandler completes processing, the requests are still processed by each HttpModule in the Pipeline, finally, the HTML is sent to the client browser.

HttpModule processes pages before and after page processing, so it does not affect real page requests. It is usually used to add some information (such as the copyright statement) to the header or tail of each page.

 

Differences between IHttpModule and IHttpHandler
1. sequence. IHttpModule first, and then IHttpHandler. Note: The Module depends on the event you have responded to. Some events run before Handler, and some run after Handler.
2. processing the request:
IHttpModule is a type of generic size. It is called no matter what file the client requests. For example, aspx, rar, and html requests.
IHttpHandler belongs to the picky eaters type. Only the file types registered by ASP.net (such as aspx and asmx) can call it.
3. IHttpHandler processes the request based on the content of the response generated by your request. The IHttpModule processes the request, such as verification, modification, and filtering. It can also process the response.


4. "HttpHandler is the main object for processing requests", but HttpModule can assign a request to a processor for execution at will!

Even the HttpModule can directly process requests without giving HttpHandler any chance to work!

 

HttpModule

Inherit the System. Web. IHttpModule interface to implement your own HttpModule class. Two methods must be implemented: Init and Dispose. In Init, you can add events to be intercepted. Dispose is used to release resources. If you have created your own resource objects in Init, release them in Dispose.

Example:


<Span style = "font-family: 'Microsoft yahei';"> public class TestHttpModule: IHttpModule
{
Public void Dispose ()
{}
Public void Init (HttpApplication context)
{
Context. BeginRequest + = new EventHandler (context_BeginRequest );
}
Void context_BeginRequest (object sender, EventArgs e)
{
HttpApplication appliction = sender as HttpApplication;
Appliction. Response. Write ("HttpModule Request URL:" + appliction. Request. Url. AbsolutePath );
}
} </Span>
<Span style = "font-family: 'Microsoft yahei';"> public class TestHttpModule: IHttpModule
{
Public void Dispose ()
{}
Public void Init (HttpApplication context)
{
Context. BeginRequest + = new EventHandler (context_BeginRequest );
}
Void context_BeginRequest (object sender, EventArgs e)
{
HttpApplication appliction = sender as HttpApplication;
Appliction. Response. Write ("HttpModule Request URL:" + appliction. Request. Url. AbsolutePath );
}
} </Span>

 

The purpose of the custom HttpModule, including global identity/permission verification, user-defined Website access/operation log records, and site monitoring and tracking for the purpose of management/debugging.

 

HttpHandler completely intercepts Http requests.
First, inherit the System. Web. IHttpHandler interface to implement your own HttpHandler class. The ProcessRequest method and IsReusable attribute of the interface must be implemented. The ProcessRequest method processes each Http Request and sends the HTML of the processing result to the output cache. The IsReusable attribute is called by the. Net Framework to determine whether the HttpHandler instance can be reused for other requests of the same type.
If you need to read or write the Session value in your HttpHandler class, you need to inherit an IRequiresSessionState interface. This interface does not have any method, but is just a tag interface. After inheriting this interface, you can access the Session in your HttpHandler and write the value in the Session.
Example:

<Span style = "font-family: 'Microsoft yahei';"> public class TestHandler: IHttpHandler
{
Public bool IsReusable
{Get {return true ;}}
Public void ProcessRequest (HttpContext context)
{
String s = context. Request. Url. AbsolutePath;
Context. Server. Execute ("default. aspx? Execute = "+ context. Server. UrlEncode (s); // URL redirection
}
}
<Add verb = "*" path = "*. shtml" type = "UI. Core. Http. TestHandler, UI"/> </span>
<Span style = "font-family: 'Microsoft yahei';"> public class TestHandler: IHttpHandler
{
Public bool IsReusable
{Get {return true ;}}
Public void ProcessRequest (HttpContext context)
{
String s = context. Request. Url. AbsolutePath;
Context. Server. Execute ("default. aspx? Execute = "+ context. Server. UrlEncode (s); // URL redirection
}
}
<Add verb = "*" path = "*. shtml" type = "UI. Core. Http. TestHandler, UI"/> </span>


A.shtml does not exist in the request.


There are two main differences between IHttpModule and IHttpHandler (in fact, we should also note this when reading MSDN ):
1. sequence. IHttpModule first, and then IHttpHandler.
2. processing the request:
IHttpModule is a type of generic size. It is called no matter what file the client requests. For example, aspx, rar, and html requests.
IHttpHandler belongs to the picky eaters type. Only the file types registered by ASP.net (such as aspx and asmx) can call it.

 


HttpModule


HttpModule Loading Method: As I mentioned earlier, "Asp.net will allocate an HttpApplication object for each request". In the initialization operation of each HttpApplication object, it will load all. httpModule registered in config. In ASP. NET, HttpRuntime manages a definition in System. for an instance of the HttpApplicationFactory class in the Web namespace, HttpApplicationFactory manages the HttpApplication object in factory mode and maintains an HttpApplication Object pool in HttpApplicationFactory so that the created HttpApplication object can be reused. Each HttpApplication object is only used to process one request at a time. Therefore, in multiple requests, the Init event of each HttpModule may be called multiple times. Many people like to perform various initialization operations here, so pay attention to the thread security issues when modifying static variable members here. In particular, if you want to execute the program initialization operation, you should put the code in Application_Start of Global. asax for processing. The Init event of HttpModule is not suitable.

Select and subscribe to appropriate pipeline events for HttpModule: This is very important. Different events are subscribed and the results will be different. The reason is also very simple. In the Asp.net runtime environment, there is not only one HttpModule. The judgment of an HttpModule may be based on the output results of other httpmodules, and in some (late) pipeline events, the output data cannot be modified. In the following example, DirectProcessRequestMoudle subscribes to the PostAuthorizeRequest event. If you subscribe to the BeginRequest event, the performance may be better. However, in the BeginRequest event, the Identity Authentication Module is not working yet, therefore, each request belongs to the "not logged on" status in this event phase.


Performance problems invisible to httpmodule
I have introduced the important advantages of HttpModule: High reusability. As long as you write an HttpModule, it can be used in any Asp.net project, which is very convenient.

However, good things cannot be abused. HttpModule can also have a negative impact on performance. The reason is also very simple: For each Asp.net request, each HttpModule executes some operation logic in the events they subscribe. These operation logics may be meaningless for some requests, but will still be executed. Therefore, computers will waste some resources to execute meaningless code.

After knowing the cause, the solution is clear:
1. Remove unnecessary HttpModule
2. In the event processor of each HttpModule, you must first determine whether the request is the one you need to process. For unrelated requests, exit immediately.

When we create an Asp.net project, if we do not make any changes, Microsoft has loaded a lot of httpmodules for us. See the following code:

Protected void Page_Load (object sender, EventArgs e) {HttpApplication app = HttpContext. current. applicationInstance; StringBuilder sb = new StringBuilder (); foreach (string module in app. modules. allKeys) sb. appendFormat (module ). append ("<br/>"); Response. write (sb. toString ());}
The output result is as follows:

 

 

There are 14 in total. Www.2cto.com
Well, most of them are definitely not used, but they are loaded. Therefore, in the events they subscribe to, their code will check all requests, the meaningless code will have the opportunity to execute. If you do not want to turn a blind eye, make similar changes in web. config to remove unnecessary modules.

<HttpModules> <remove name = "Session"/> <remove name = "RoleManager"/>

 

From hi_dzj's column

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.