Hello Friends, In this tutorial we are going to understand HTTPHandler in ASP.NET.  ASP.NET handles all the HTTP requests coming from the user and respond to that request. ASP.NET framework also capable of how to process requests based on extension, for example, It can handle request for .aspx, .ascx and .txt files, etc

After completing this tutorial you will be able to understand:

  • HTTPHandler in ASP.NET.

What is HTTPHandler in ASP.NET?

HTTPHandlers is a class that implements IHTTPHandler interface. It is used to handle HTTP requests based on extension, for example, it can handle requests for .aspx, ascx and .txt files etc.
It is an extension based processor that processes the request based on the file extension. When a request comes from the browser HTTP Handler checks the extension to see if it can handle the request and respond to request after performing some predefined functionality.

HTTPHandler

Following are the methods of HTTP Handler.

ProcessRequest:  Used to call Http Requests.

IsReusable:  To check the reusability of the same instance handler with a new request of the same type.

Implementing the HTTPHandler:

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

public void ProcessRequest(HttpContext context)
{

}
}

Configuring HTTPHandler:

.Net provodes <httpHandlers> and <add> nodes for adding HTTPhandlers to our Web applications. In fact the handlers are listed with <add> nodes in between <httpHandlers> and </httpHandlers> nodes. Here is the example for registering HTTPHandler to our web application.

<httpHandlers>
<add verb="supported http verbs" path="path" type="namespace.classname, assemblyname" />
<httpHandlers>

Example of HTTP Handler:

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

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";

if (context.Request.RawUrl.Contains(".cspx"))
{
string newUrl = context.Request.RawUrl.Replace(".cspx", ".aspx");
context.Server.Transfer(newUrl);
}
}
}

Registering HTTPHandler in web.config:

<httpHandlers>
<add verb="*" path="*.cspx" type="CspxHandler"/>
</httpHandlers>

View More:

Conclusion:

Hope you understand why we use HTTPHandler in ASP.NET. Your Feedback, comments and suggests are always welcome to me.

Thank You.

Leave a Comment