WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 56 users


WorldofASP.NET >> ASP.NET >> HttpHandlers and HttpModules

Building HTTP Handler

knowledge for HTTP handler and HTTP runtime, how to create custom HTTP Handler
Published Date : 30 Nov 2008
Author : siewlee
Language : C#
Platform : .NET
Technology : ASP.NET
Views : 3154
Rating : (0 votes so far)



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 handler

Hypertext 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("&lt;h3> Hello " + strName + "&lt;/h3>");//  display a message in a Web browserContext.Response.Write("&lt;b>This is an  HTTPHandler sample project</b>");
Context.Response.Write("&lt;hr width="260" align="left"&gt;<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("&lt;b>Browser capabilities:</b><br>");
Context.Response.Write("Browser name : " + hBrowser.Browser + "&lt;br>");
Context.Response.Write("Browser version : " + hBrowser.Version + "&lt;br>");
Context.Response.Write("Browser platform : " + hBrowser.Platform + "&lt;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 + "&lt;br>");
Context.Response.Write("Virtual path : " + vPath + "&lt;br>");// check the client is connected to the serverBoolean connect = Context.Response.IsClientConnected;
String connectStr = connect.ToString();
Context.Response.Write("&lt;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




Other Related and Popular Articles :

Using HttpHandlers and HttpModules in your ASP.NET Websites
How to use HttpHandlers and HttpModules on your ASP.NET websites and why you need to use them

Displaying images in ASP.NET using HttpHandlers
This article explains about HttpHandlers and how you can use them to display images in ASP.NET.

ASP.NET HTTP Modules:
HTTP Modules use to intercept HTTP requests for modifying or utilize HTTP based requests according to need.


Author Profile : siewlee

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

Leave New Comments


Article Content copyright by siewlee
Everything else Copyright © by WorldofASP.NET 2010

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here







Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2010 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | ASP Hosting | ASP.NET Hosting | Phone Card | Calling Card |Stock Investing