Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Responding to Specialized HTTP Request with HTTP Handlers and Modules

Micael George

HTTP handlers and modules are a great way to handle HTTP request sent to the server. They can be used to write a specialized image server, a process to handle custom level of security, or a file processor. This was the case for a previous project I worked on; we used HTTP Handlers for the generation of different types of documents.

So what exactly are HTTP Handlers and HTTP Modules? An HTTP handler is an endpoint on the server that responds to requests for different resources. Handler code executes when an HTTP request for a specific resource is made to the server. In contrast, an HTTP module is code that gets called for every request or response routed to the application. Modules are ideal in situations when you want to add additional processing of tasks along with the predefined ASP.NET event handling for requests.

Creating a Custom HTTP Handler

A simple synchronous HTTP handler, i.e. a handler called immediately when the request occurs, can be implemented by implementing the IHttpHandler interface, which requires you to implement the IsReusable property and ProcessRequest method. The IsReusable property is used to indicate whether the handler can be reused; setting the property to true will put the handler into a pool to help increase performance across requests. The ProcessRequest method is used to execute code meant to produce a response to the HTTP request. Below is an example of a custom handler.

Custom Handler

Finally, once the hander is created, you must register the handler. There are two ways to register a handler, either you can configure IIS to send request to ASP.NET and the application, or you can add configuration code to the Web.config file to register the custom handler when the application is deployed. The latter approach can be done simply by doing the following:

Web.config Registration Using IIS 6.0

Web.config Registration Using IIS 7.0

Creating a Custom HTTP Module

An HTTP module is created by implementing the IHttpModule interface. Once the IHttpModule interface is implemented, the module is then registered by us the Web.config file, similar to the way HTTP handlers are registered. When the module is created, its Init method is called. You then override the IHttpModule.Init method to indicate the applications events to which you want to subscribe, which are typically application life cycle events such as Beginequest, AuthenticateRequest, ProcessRequest, and others. Finally, the code is then called when the aforementioned events fire for each request.  Below is an example of a custom module and the Web.config registration.

Custom Module

Web.config Registration Using IIS 6.0

Web.config Registration Using IIS 7.0

In closing, after encountering modules and handlers in a previous project and also as one of the objectives for the MCTS 70-515, I was prompted to write this blog to provide a basic overview of them. HTTP Handlers are used to respond to specific request for resources, whereas HTTP modules are used to handle every request or response to the application. HTTP Modules and Handlers are straightforward ways to handle specialized HTTP request, but one thing to note before concluding is that ASP.NET also provides a mechanism for creating asynchronous handlers. This increases the performance of the page and can be done by implementing the IHttpAsyncHandler interface.

1 Comment. Leave new

Ian Andrews City

Responding to Specialized HTTP Request with HTTP Handlers and Modules

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>