ASP. HttpModule and HttpHandler

Source: Internet
Author: User

Asp. NET when processing HTTP request, using pipeline (pipeline) method, the request is processed by each HttpModule, and then arrives httphandler,httphandler after processing, is still processed by each HttpModule in pipeline, and the HTML is finally sent to the client browser.
HttpModule will process the page before and after the page is processed, so it will not affect the actual page request. It is usually used to add some information (such as a copyright notice) to the head or tail of each page.
The difference between IHttpModule and IHttpHandler
1. Sequencing. First IHttpModule, then IHttpHandler. Note: module depends on which event you are responding to, some of which are running before handler, some are running after handler
2. On the processing of the request:
IHttpModule is a size-all type that calls to it regardless of what file the client requests, such as Aspx,rar,html's request.
IHttpHandler is a picky eater, and only the file types that are registered by ASP (for example, Aspx,asmx, and so on) will be called.
3.IHttpHandler generates the content of the response according to your request, ihttpmodule the request, such as validation, modification, filtering, and so on, and can also handle the response
HttpModule inherits the System.Web.IHttpModule interface and implements its own HttpModule class. There are two ways to implement an interface: Init and Dispose. In Init, you can add events that need to be intercepted, Dispose is used for resource deallocation, and if you create your own resource object in Init, release it in Dispose.
Example: 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 requested URL:" + appliction. Request.Url.AbsolutePath);
Custom HttpModule use, with global identity/authorization verification, custom site access/operation log records, management/debugging and other purposes to monitor the site tracking, etc.
The HttpHandler is a complete interception of HTTP request.
First, inherit the System.Web.IHttpHandler interface and implement your own HttpHandler class. The ProcessRequest method and the IsReusable property of the interface must be implemented. The ProcessRequest method completes the processing of each HTTP request, sending the HTML of the processing result to the output cache. The IsReusable property is called by the. Net framework to determine whether the instance of this HttpHandler can be reused for other request processing of the same type.
If you need to read or write session values in your HttpHandler class, you need to inherit an interface irequiressessionstate. There is no method for this interface, just a token interface. After inheriting this interface, you can access the session in your HttpHandler, and you can write values in the session.
Example: 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 redirect
<add verb= "*" path= "*.shtml" type= "UI. Core.http.testhandler,ui "/>
The a.shtml may not exist for the request

ASP. HttpModule and HttpHandler

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: 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.