How to work with HTTPHandlers in ASP.Net

HTTPHandler is a low level request and response API in ASP.Net for injecting pre-processing logic to the pipeline based on file extensions and verbs

HttpHandler

HttpHandler

An HTTPhandler may be defined as an end point that is executed in response to a request and is used to handle specific requests based on extensions. The ASP.Net runtime engine selects the appropriate handler to serve an incoming request based on the file extension of the request URL. On the contrary, an HttpModule is a component that is part of the ASP.Net request processing pipeline and is called on every request that is made to your application. Note that the basic objective of both HTTPhandlers and HttpModules is to inject pre-processing logic to the pipeline.

Assume that your application needs to serve images of varying sizes - you can take advantage of a custom HTTPhandler to resize those images and send the response back. Another scenario where you might want to use a custom HTTPhandler is when you would like to execute some pre-processing logic in your application based on the extensions. Although you can do almost anything you can do with HTTPhandler also with your ASP.Net page, HTTPhandlers are much more portable and reusable than your web pages.

When a request for a resource comes to the ASP.Net Engine, the ASP.Net Worker Process in turn instantiates the appropriate HTTPhandler to server the request based on the extension. An HTTPhandler in ASP.Net is a class that implements the IHTTPhandler interface. Incidentally, the IHTTPhandler interface is available in the System.Web namespace. Note that the PageHandlerFactory implements the IHTTPhandlerFactory interface and contains a method called GetHandler which in turn is responsible for returning the appropriate handler to server the particular request.

The MSDN states: "An ASP.Net HTTPhandler 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 via the page handler."

Creating a custom HTTPhandler

In this section we will explore on how we can build a custom HTTPhandler in ASP.Net. To build a custom HTTPhandler, create a class that implements the IHTTPhandler as shown in the code snippet below.

namespace CustomHTTPhandler

{

   public class IDGCustomHTTPhandler : IHTTPhandler

   {

       public bool IsReusable

       {

           get { return false; }

       }

       public void ProcessRequest(HttpContext context)

       {

           throw new NotImplementedException();

       }

   }

}

Note that your custom HTTP handler should have a property called IsReusable and a method called ProcessRequest. While the former is used to specify whether the handler can be reused, the latter is a method that does the actual processing for you. In essence, any custom HTTPHandler should implement the IHttphandler interface and define these two members.

Registering your handler

The mapping information for the HTTPhandlers are available in the configuration files. Here's how a section of your machine.config file might look like.

<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />

<add verb="*" path="*.config" type="System.Web.HttpForbiddenHandler" />

<add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory" />

Now, you would also need to let the runtime know when your custom HTTPhandler should be invoked. Where should you specify this? Well can specify such details in the web.config file. You can add and remove HTTPhandlers using the <httphandlers> configuration section in your applications configuration file. Here's how you can register your handler in the application's web.config file.

<httpHandlers>

   <add verb="*" path="*.idgaspx" type="IDGCustomHTTPhandler"/>

</httpHandlers>

So, what did we do here? We just registered our handler and specified that if any request for an extension of .idgaspx arrives, such requested should be routed to the custom Http Handler named IDGCustomHTTPhandler.

Note that unlike .aspx web pages, the HTTPhandlers don't have visual elements. You can create your HTTPhandlers in a custom library and then reuse them as and when they are needed.

Asynchronous HTTPHandlers

The newer versions of ASP.Net provide support for asynchronous Http handlers. You can take advantage of async/await and the TPL to build asynchronous HTTPhandlers in ASP.Net. To create a custom asynchronous HTTPhandler, you should inherit the HttpTaskAsyncHandler class. The HttpTaskAsyncHandler abstract class in turn implements the IHttpAsyncHandler and IHTTPhandler interfaces. The following code snippet illustrates how our custom asynchronous HTTPhandler looks like at first glance.

   public class IDGCustomHTTPhandler : HttpTaskAsyncHandler

   {

       public override Task ProcessRequestAsync(HttpContext context)

       {

           throw new NotImplementedException();

       }

   }

This article is published as part of the IDG Contributor Network. Want to Join?

To comment on this article and other InfoWorld content, visit InfoWorld's LinkedIn page, Facebook page and Twitter stream.
From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.