Work with HTTP Handlers

The HTTPHandlers can be considered as endpoint of a request. When a request comes in to the IIS server, it passes through a set of HTTP modules and finally it ends up at a particular HTTP handler.

HTTPHandlers.gif

When we request for an aspx page through the browser, the ASP.NET page handler processes the request to return the page. Here, the ASP.NET page handler is nothing but an http handler.

So basically, an httphandler is a code that runs in response to a client request. In ASP.NET we have

  • The page handler for aspx page requests,
  • Web service handler for .asmx files
  • User control handler for .ascx etc which are built in HTTP handlers

In addition to these, we can write our custom http handlers to handle requests for particular file extensions.

In this article we are going to write an HTTPHandler that handles requests made for .test files. Which means that if we request for a page www.(SomesiteName.com/SomePage.test , this request is going to be handled by our custom code. So lets get started.

HTTPHandlers1.gif

Class for our custom HTTP handler : 

An HTPP handler can be synchronous or asynchronous.To write a custom HTTP handler we need to write a class that implements IHttpHandler interface to create a synchronous handler and IHttpAsyncHandler to create an asynchronous handler. We write a ProcessRequest() that contains the code that is to be executed and sends out a response to the client. The ProcessRequestmethod() takes the HTTPContext object as a parameter. This context object reprsents the context which is specific to our request.

Create a ASP.NET web site using visual studio. Add a class file and name it "httpHandler". Now as mentioned earlier this class implements IHttpHandler interface.

    public class httpHandler : IHttpHandler
    { 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("This is a custom http handler for .test files");
        }
        public bool IsReusable
        {
            get { return false; }
        }
    }


In the ProcessRequest() method, we have simply written a line on the web page. We also need to implement IsReusable property which returns a Boolean value. If this value is true, the object of this call is pooled and reused.

Registering the HTTP handler : 

Now we are done writing with our custom HTTP handler. We need to register it so that it starts working for page requests. This is done in Web.config file.

Open your web.config file and search for <httpHandlers> section. You will see some handlers already added to the section.

Add the following entry

<httpHandlers>
    <
add verb="*" path="*.test" type="httpHandler"/>
</httpHandlers>


The path attribute specifies the extension for which this handler should execute. 

*.test : means execute this code for all the files ending with ".test" extension.

We are done. Easy enough ? 

Let us test this now. Run your ASP.NET web site project and it will start the default.aspx page. Change the URL to some URL ending with ".test" and the result will be as below.

HTTPHandlers2.gif

Here you can see our custom code was executed for a request of .test extension.

Similarly we can also write HTTP handler to handle some other extension or a particular URL. In the next article we are going to see in depth the use of IsReusable property.

Next Recommended Reading Redirecting from HTTP to HTTPS