Introduction
This article briefly describe HTTP handler, HTTP runtime provided in ASP.NET, the interfaces and classes involved in creating HTTP handlers and guide you how to create a custom HTTP handler.Main
HTTP handlerHypertext Transfer Protocol (HTTP) is an application-level protocol that is responsible for establishing a connection between a Web server and a client (web browser) and transmitting the information requested by the client. ASP.NET works by transmitting the client requests to user-defined HTTP handler objects called HTTP handlers. You can create your own HTTP handler by implementing a .NET interface called IHttpHandler and then bind a specific URL request to the handler for handling specific requests. However, if the specific URL request is not mapped to the handler, the default handler of ASP.NET handles it.
HTTP runtime
HTTP runtime is built on the Common Language Runtime (CLR) and provides an environment for processing the requests, such as receiving requests from web client, resolving the address specified in the URL, and send the request to appropriate application for further processing of the request. It is comprised of HTTP modules and HTTP handlers. When you enter a URL in a browser, the browser builds an HTTP request by various methods (Get, Post or Head) and sends it to the address specified in the URL. For example, a web client makes a request that results in executing a Web application, the request is passes to HTTP modules and HTTP modules enable a Web application to perform the tasks and then the request is sent to HTTP handler. HTTP handler receives the request, get the required data and send the data to the web client.
Interfaces and Classes Used to Create HTTP Handlers
The System.Web namespace contains classes and interfaces that enable you to handle the communication between Web browsers and Web servers. Let’s look at the IHttpHandler and IHttpHandlerFactory interfaces and few classes provided by System.Web namespace.
(I) IHttpHandler interface
When you create a class that implements the IHttpHandler interface, you need to implement a method (ProcessRequest) and a property (IsResuable) of this interface. ProcessRequest is called whenever an HTTP request is made and IsReusable is a read only property that gets a Boolean value indicating whether the IHttpHandler instance can be reused for other Web requests.
(III) HttpContext , HttpRequest and HttpResponse classes
HttpContext class provides reference to the built-in server objects to process Web requests. It provides the properties to get the HttpApplicationState object, SessionState object, HttpRequest object, HttpResponse object,and HttpServerUtility object associated with the current HTTP request.
HttpRequest class enables you to handle communication from a Web browser to a Web server. You can use this class to access the data supplied by Web clients during HTTP request such as information related to the capabilities of the browser, application root path of the current application that is executing on a server, virtual path of the currently HTTP request and etc.
HttpResponse class enables you to handle communication from a Web server to a browser. It is responsible for sending the output from the server to the browser such as gets or sets the character set of the output from the server, return a Boolean value that indicates whether or not the Web client is connected to the server and etc.
Create HTTP handler class
Steps:
1. Create a class library project and set a reference to the System.Web.dll assembly
2. Add System.Web namespace, IHttpHandler interface, ProcessRequest method and IsResuable property to the class
3. Build the project to create the DLL file for the handler class
4. Create a Web application project and add reference to this DLL file
5. To use the DLL in the web application, you need to add <httpHandlers> section to the Web.config file
This sample is displays a hello message to the user and accesses the request and response information when an HTTP request is made for a Web page:
Sample code for handler class:
using System;using System.Collections.Generic;using System.Text;using System.Web;namespace CLHandler
{
public class MyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext Context)
{
String strName;// get the user name from the Request property of the Context objectstrName = Context.Request.QueryString["Name"].ToString();
Context.Response.Write("<h3> Hello " + strName + "</h3>");// display a message in a Web browserContext.Response.Write("<b>This is an HTTPHandler sample project</b>");
Context.Response.Write("<hr width="260" align="left"><br>");// using the Browser property of the Request object to get an object of the HttpBrowserCapabilities class HttpBrowserCapabilities hBrowser = Context.Request.Browser;// display the name and version of the browserContext.Response.Write("<b>Browser capabilities:</b><br>");
Context.Response.Write("Browser name : " + hBrowser.Browser + "<br>");
Context.Response.Write("Browser version : " + hBrowser.Version + "<br>");
Context.Response.Write("Browser platform : " + hBrowser.Platform + "<br>");// get the physical file system path and virtual application root pathString pPath;
String vPath;
pPath = Context.Request.PhysicalApplicationPath;
vPath = Context.Request.ApplicationPath;
Context.Response.Write("Physical path : " + pPath + "<br>");
Context.Response.Write("Virtual path : " + vPath + "<br>");// check the client is connected to the serverBoolean connect = Context.Response.IsClientConnected;
String connectStr = connect.ToString();
Context.Response.Write("<br>Client connection status : " + connectStr);
}
public Boolean IsReusable
{
get
{
return true;
}
}
}
}
Add the following code to Web application’s Web.config file:
connectionStrings/>
<system.web>
<httpHandlers>
<add verb="*" path="Default.aspx" type="CLHandler.MyHandler, CLHandler"/>
</httpHandlers>
Finally, test the handler by passing your name in the Name variable as QueryString, for example
http://localhost:1032/HTTPHandlerApp/Default.aspx?Name=MyName
Below is the output of the sample:

Download Source Code
Download Source Code