Create HTTP Handler in Asp.Net

Whenever you request a page in asp.net, page class executes and actually page class is HTTP Handler. That means in .net handler handles the request based on the extension. An HTTP handler is a .Net class executes whenever a request for particular file.

 

There are several predefined handlers are there in .Net, for example Page http handler handles page requests, TraceHandler displays page level trace information and ForbiddenHandler restrict the codebehind file page requests(.aspx.cs requests).

 

Here we discuss about simple http handler which displays the warning message if user requests any page with extension .abc.

 

To create HTTP Handler, we have to implement IHTTPHandler interface which has ProcessRequest method which is actually process the request and IsReusable propery to decide whether same HTTP Handler handles the multiple request.

 

Create the ABCHandler HTTP Handler, add the condition to handles the .abc extension requests as shown below.

 

class ABCHandler:IHttpHandler

{

   public bool IsReusable

    {

        get { return true; }

    }

 

 

    public void ProcessRequest(HttpContext context)

    {

        string excuttablepath = context.Request.AppRelativeCurrentExecutionFilePath;

 

        if (excuttablepath.ToLower().Contains(".abc"))

        {

            context.Response.Write("Sorry, This type of pages will not process");

        }

    }

}

 

As shown above, implement the IHttpHandler interface and check for the .abc extension in ProcessRequest method. Set the IsReusable property to true that means same HTTP Handler handles the multiple requests.

 

After creating the HTTP Handler, we have to register this handler in httpHandlers section of Web.Config file as shown below.

 

<httpHandlers>

    <addverb="*"path="*.abc"type="ABCHandler"/>                         

</httpHandlers>

 

Now add sample file sample.abc to the project and request for that file. The output displays as shown below.

 

 

 

 

 

 

 

Generally IIS(Internet Information Server) do not send all requests to Asp.Net Framework, it will send requests which can handle by Asp.Net Framework. So, our custom handler will not work if you place your web site in IIS without registering your extension.

 

To register extension in IIS for particular web site, go to particular website or virtual directory, select properties and click on Configuration under Directory tab. Select Mapping tab, click on Add and enter ASP.NET ISAPI DLL path in Executable field(generally it  is “c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll”)  and extension .abc in Extension field, Then click Ok and close the window.

 

 

 

Now request the sample.abc page, error message displays as we are handling in ABCHandler handler. In This way we can create custom HTTP Handler in asp.net and we can handle any page requests.

 

                                                                                                            HTTPHandlerExp.zip (3.07 kb)