Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to create HttpHandler in ASP.NET

Author: Mike Green
Download Source Code :

ASP.NET requests are handled by HttpHandler. This article will explain about HttpHandler and how we can create custom HttpHandler.


What is HttpHandler?
HttpHander is a component in ASP.NET to service incoming requests to server. HttpHandlers run as processes in response to requests made to ASP.NET application. All HttpHandlers implement IHttpHandler interface. IHttpHandler interface is located in System.Web namespace. HttpHandler can be directly called in URL.
ProcessRequest() Method
All ASP.NET requests are processed in ProcessRequest() method. All work regarding request processing is done in this method. ProcessRequest() method allows to process request for custom HttpHandler. It implements IHttpHandler interface.
IsReusable Property
IsReuseable Property of HttpHandler defines whether your handler is reused or not. It specifies that your IHttpHandler instance is available for one request or more.
ASP.NET has following five common Http Handlers.
Page Handler (.aspx)
Page Handler handles Web Pages requests. It is the default Handler for all aspx Web Pages.
Web Service Handler (.asmx)
Web Service Handler handles Web Service requests. It is default handler for all asmx Web Service pages.
User Control Hander (.ascx)
User Control handler handles Web user control requests. It is the default handler for all ascx user control pages.
Trace Handler (trace.axd)
Trace Handler handles trace functionality. It is responsible to display trace information.
Generic Web Handler (.ashx)
Generic Web Handler is the default handler for all Web Handlers that don’t have UI.
Create Your HttpHandler
  1. Open MS Visual Studio 2010
  2. File > New > Project > Visual C# or Visual Basic > ASP.NET Web Application. Rename the Application as “SimpleHandler”
  3. Project > Add Class. Rename the class as “HandlerClass”
  4. Write code below in HandlerClass

    namespace SimpleHandler{
        public class HandlerClass : IHttpHandler {
            public bool IsReusable {
                get { return false;}
            public void ProcessRequest(HttpContext context) {
                HttpResponse response = context.Response;
                response.Write("This is our simple Http handler");
    Public Class HandlerClass
        Implements IHttpHandler
        Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
                Return False
            End Get
        End Property
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Dim response As HttpResponse = context.Response
            response.Write("This is our simple Http handler")
        End Sub
    End Class

    Our HandlerClass implements IHttpHandler interface. Write IsReusable Property that implements IHttpHandler interface. It is a Boolean property that returns true to reuse the IHttpHandler instance for more than one request and false to use only once. Write ProcessRequest() method to write your request processing functionality. ProcessRequest() Method implements IHttpHandler. 
  5.  Now configure your web.config file for HttpHandler. Write <HttpHandlers> tag in between <system.web> tag.

      <add verb="*" path="OurHandler.handler" type="SimpleHandler.HandlerClass, SimpleHandler"/>

    We have written three attributes in <add> tag.
    verb: we can mention request as HTTP POST or HTTP GET in “verb” attribute. Here “*” means any type of request.
    path: In “path” attribute, we can write path which we will write in URL to invoke the HttpHandler.
    type: we have to provide the handler class and dll file name  in “type” attribute. First part of the attribute value indicates the full qualified class name and second part is the dll assembly that contains the handler class.
  6. Now compile your project and run in browser. You have to give full path in URL to invoke your handler. When you run the project you can see “localhost:portnumber” in URL. Write below URL after portnumber.
    Press enter to see result.
Add Article Comment:
Name :
Email Address :
Comments :
<< How to use LinqDataSource in ASP.NET

Disclaimer - Privacy
© 2002-2018