Differences between 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.

HttpModule inherits the System. Web. IHttpModule interface to implement its 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: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 );
}
}

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: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"/>

A.shtml does not exist in the request.

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.