Introduction to ASP.NET HttpModule

No.of Views1762
Bookmarked0 times
By  jalpesh   On  13 Jan 2011 10:01:12
Tag : ASP.NET , HttpModules
I know this article may sound like very beginner level. But I have already posted two topics regarding HTTP Handler and HTTP module and this will explain how http module works in the system.In this article I am going to explain about HTTP Module in detail
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



I know this article may sound like very beginner level. But I have already posted two topics regarding HTTP Handler and HTTP module and this will explain how http module works in the system. I have already published What is the difference between HttpModule and HTTPHandler here. Same way I have published about an HTTP Handler example here as people are still confused with it. In this article I am going to explain about HTTP Module in detail.

What is HTTP Module

As we all know that when ASP.NET Runtimes receives any request it will execute a series of HTTP Pipeline extensible objects. HTTP Module and HTTP handler play important role in extending this HTTP Pipelines. HTTP Module are classes that will pre and post process request as they pass into HTTP Pipelines.  So It’s one kind of filter we can say which will do some procession on begin request and end request.

If we have to create HTTP Module we have to implement System.Web.IHttpModule interface in our custom class. An IHTTP Module contains two method dispose where you can write your clean up code and another is Init where your can write your custom code to handle request. Here you can your event handler that will execute at the time of begin request and end request.

Let’s create an HTTP Module which will just print text in browser with every request. Here is the code for that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Experiment
    public class MyHttpModule:IHttpModule
        public void Dispose()
            //add clean up code here if required
        public void Init(HttpApplication context)
            context.BeginRequest+=new EventHandler(context_BeginRequest);
            context.EndRequest+=new EventHandler(context_EndRequest);  
        public void context_BeginRequest(object o, EventArgs args)
            HttpApplication app = (HttpApplication)o;
            if (app != null)
                app.Response.Write("<h1>Begin Request Executed</h1>");
        public void context_EndRequest(object o, EventArgs args)
            HttpApplication app = (HttpApplication)o;
            if (app != null)
                app.Response.Write("<h1>End Request Executed</h1>");

Here in above code you can see that I have created two event handler context_Beginrequest and context_EndRequest which will execute at begin request and end request when request are processed. In this event handler I have just written a code to print text on browser.

Now In order enable this HTTP Module in HTTP pipeline we have to put a settings in web.config  HTTPModules section to tell which HTTPModule is enabled. Below is code for HTTPModule.

        <compilation debug="true" targetFramework="4.0" />
        <add name="MyHttpModule" type="Experiment.MyHttpModule,Experiment"/>

Note:If you are using IIS7 or later version, your configuration file would like below,

    <modules >
      <remove name="MyHttpModule" />     
       <add name="MyHttpModule" type="Experiment.MyHttpModule,Experiment"/>

Now I just have created a sample webform with following code in HTML like following.

<form id="form1" runat="server">
    <B>test of HTTP Module</B>

Now let’s run this web form in browser and you can see here it the output as expected. 

Image Loading

Hopes help and thank you for reading.

Sign Up to vote for this article
About Author
Occupation-Software Engineer
Member Type-Gold
Joined date-08 May 2010
Home Page-
Blog Page-
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
Other popularSectionarticles
By:ChinnaDate Of Posted:1/17/2011 7:04:22 AM
Even it has basic info. It has many things missing and lot more confusion
Hello, You have missed may things here. As you said "Any request". Not all requests go through the pipline. What if the request is for an image or a file in the server? before it is submitted to pipline there soime things happens to the request. there is a queue? who puts it in queue. what if if the request is encoded? In the Http pipline where exactly this modle comes in to picture? for IIS6 and II7 there is lots of difference in Http pipline. Thanks, Chinna
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top