Explain HTTP Handlers and HTTP Modules - Interview Question

Posted by: Pravinkumar Dabade , on 3/20/2016, in Category ASP.NET
Views: 41059
Abstract: HTTP handler vs HTTP module - A frequently asked ASP.NET Interview question

When a client makes a request for a resource located on the server in an ASP.NET application, each request is handled by the HTTP Handlers. Microsoft ASP.NET has number of built-in HTTP Handlers which serves different files like .ASPX, .ASMX etc. Based on the extension of the file, the appropriate HTTP Handlers gets loaded which is mapped to the extension and is responsible for processing the ASP.NET request.

Custom HttpHandlers

You can also create your own custom HTTP Handlers and register it for request handling. You can register the HTTP Handlers in a Web.Config file using <HttpHandlers /> element. However, the registration will always be based on the version of IIS [Old Versions/IIS 7 Classic Mode/Integrated Mode].

Let's create a small example of custom HTTP Handlers. You can implement a custom HTTP Handler using a simple class which implements an interface IHttpHandler. This interface provides one method ProcessRequest() and a property IsReusable.

  • ProcessRequest() - This is a method which gets called when the request is received. Inside this method, you can call the HttpContext object which is passed as a parameter to the method. Using this object you can access the Request, Response and Server objects for implementing the processing logic.
  • IsReusable Property - When this handler is called, the ProcessRequest method will process the request. If the IsReusable property is set to true, the same handler will be used for processing other requests of the same type. If it is false, then after the request is processed, the handler object gets destroyed.

Let's create a custom handler as shown below -

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

    public void ProcessRequest(HttpContext context)
        context.Response.Write("<h1 style='Color:#000066'>WelCome To Custom HttpHandler</h1>");
        context.Response.Write("HttpHandler processed on - " + DateTime.Now.ToString());
        using (StreamWriter SW=new StreamWriter(@"E:\HandlerMessages.txt",true))
            SW.WriteLine("The message date time is - " + DateTime.Now.ToString());

Now let's configure our custom HttpHandler into Web.Config file as shown below -

        <add verb="*" path="*.curry" type="CustomHandlerModuleExample.CustomHandler"/>

Now add a simple text file with extension ".curry" and browse the ".curry" extension file. It should look like the following-


So in simple words, an HttpHandler is frequently associated with a specific extension, and a practical usage includes dynamic image generation or modification.


What is an HTTP Module

Now let's explore the HttpModule. HttpModule is another part of request processing of ASP.NET. In a single request processing, there can be more than one modules which gets executed. HttpModules take part in processing of the request by handling the Application events. There are number of events which you can handle during the HttpModule processing. For example - BeginRequest(), EndRequest(), AuthenticateRequest() etc.

You can also create custom HttpModules. You can create a custom HttpModule using a simple class which implements IHttpModule interface. This interface provides two method -

  • Init() - This method takes a HttpApplication object as parameter which allows the HttpModule to register the events.
  • Dispose() - The logic of cleanup can be written in this method which will get executed before garbage collection.

HttpModules can be registered in Web.Config using <httpModules/>

Let's create a simple HttpModule as shown below which creates a log file on C: -

public class CustomHttpModule:IHttpModule
    public void Init(HttpApplication context)
        context.BeginRequest += new EventHandler(this.context_BeginRequest);
        context.EndRequest += new EventHandler(this.context_EndRequest);
    public void context_EndRequest(object sender, EventArgs e)
        StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true);
        sw.WriteLine("End Request called at " + DateTime.Now.ToString()); 
    public void context_BeginRequest(object sender, EventArgs e)
        StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true);
        sw.WriteLine("Begin request called at " + DateTime.Now.ToString()); 
    public void Dispose()


Let's register the custom HttpModule into the Web.Config file as shown below -

      <add name="DotNetCurryModule" type="CustomHttpModule"/>

Now call the same file in a browser "Hello.curry" and check the file on C drive. You will find a requestLog.txt.

So in simple words, an HttpModule will execute for every request of your application, irrespective of the extension used. Http Modules are generally used for security, statistics, logging etc.

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar [attherate] gmail[dot]com

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!





Free DNC .NET Magazine



jQuery CookBook