Explain HTTP Handlers and HTTP Modules - Interview Question
Posted by: Pravinkumar Dabade ,
on 3/20/2016,
in
Category ASP.NET
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());
SW.Close();
}
}
}
Now let's configure our custom HttpHandler into Web.Config file as shown below -
<httpHandlers>
<add verb="*" path="*.curry" type="CustomHandlerModuleExample.CustomHandler"/>
</httpHandlers>
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());
sw.Close();
}
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());
sw.Close();
}
public void Dispose()
{
}
}
Let's register the custom HttpModule into the Web.Config file as shown below -
<httpModules>
<add name="DotNetCurryModule" type="CustomHttpModule"/>
</httpModules>
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
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