8.19.2012

Difference Between HttpHandler and HttpModule

Difference between HttpHandler and HttpModule
S.No
HttpHandler
HttpModule
1
Meaning:

An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. We can create our own HTTP handlers that render custom output to the browser.In order to create a Custom HTTP Handler,we need to Implement IHttpHandler interface(synchronous handler) or
Implement IHttpAsyncHandler(asynchronous handler).
Meaning:

An HTTP module is an assembly that is called on every request that is made to our application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules examine incoming and outgoing requests and take action based on the request.
2
When to use HTTP handlers:

RSS feeds: To create an RSS feed for a Web site, we can create a handler that emits RSS-formatted XML. We can then bind a file name extension such as .rss to the custom handler. When users send a request to your site that ends in .rss, ASP.NET calls our handler to process the request.
Image server: If we want a Web application to serve images in a variety of sizes, we can write a custom handler to resize images and then send them to the user as the handler’s response.

When to use HTTP modules:

Security: Because we can examine incoming requests, an HTTP module can perform custom authentication or other security checks before the requested page, XML Web service, or handler is called. In Internet Information Services (IIS) 7.0 running in Integrated mode, we can extend forms authentication to all content types in an application.
Statistics and logging: Because HTTP modules are called on every request, we can gather request statistics and log information in a centralized module, instead of in individual pages.
Custom headers or footers: Because we can modify the outgoing response, we can insert content such as custom header information into every page or XML Web service response.

3
How to develop an ASP.NET handler:

All we need is implementing IHttpHandler interface

public class MyHandler :IHttpHandler
{
public bool IsReusable
{
get { return false; }
}

public void ProcessRequest(HttpContext context)
{

}
}
How to develop a Http Module:

All we need is implementing System.Web.IHttpModule interface.

public class MyHttpModule : IHttpModule
{
public void Dispose()
{

}

public void Init(HttpApplication context)
{
//here we have to define handler for events such as BeginRequest ,PreRequestHandlerExecute ,EndRequest,AuthorizeRequest and ....

}

// you need to define event handlers here
}
4
Number of HTTP handler called:

During the processing of an http request, only one HTTP handler will be called.
Number of HTTP module called:

Whereas more than one HTTP modules may be called.
5
Processing Sequence:

In the asp.net request pipe line ,http handler comes after http Module and it is the end point objects in ASP.NET pipeline.
Processing Sequence:

In the asp.net request pipe line, http Module comes first.
6
What it does actually?

HTTP Handler actually processes the request and produces the response
What it does actually?

HTTP module can work on request before and on the response after HTTP Handler
7
HTTP Handler implement following Methods and Properties:

Process Request: This method is called when processing asp.net requests. Here you can perform all the things related to processing request.
IsReusable: This property is to determine whether same instance of HTTP Handler can be used to fulfill another request of same type.
Http Module implements following Methods and Properties:

InIt: This method is used for implementing events of HTTP Modules in HTTPApplication object.
Dispose: This method is used perform cleanup before Garbage Collector destroy everything.
8
Summary:

If we need to create a request handler, for example we may want our own code to handle all .jpg image file requests like: http://mysite.com/filename.jpg, then we need to use HttpHandlers for this purpose.
Summary:

If we want to modify a certain request, like we may want to perform some custom logic behind the scenes whenever user requests pages like mysite.com/default.aspx, we need to use HttpModules. We can create multiple HttpModules to filter any request.

2 comments:

  1. I found your blog to be very informative and interesting. On similar lines you can also check outhttp://sco.lt/8Dtewz which is also a very good blog on this very topic. Request you to continue writing on varied topics as we would like to read.

    ReplyDelete