Read this MSDN Article for better understanding concept of HTTPModule & HTTPHandler.
http://msdn.microsoft.com/en-us/library/bb398986.aspx
Before starting have a look in the Web.config in the location
C:\Windows\Microsoft.NET\Framework\versionnumber\CONFIG\Web.config
You can see the existing HTTPModules in the framework
<system.web>
<httpModules>
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/>
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/>
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule"/>
<add name="RoleManager" type="System.Web.Security.RoleManagerModule"/>
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/>
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/>
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule"/>
<add name="Profile" type="System.Web.Profile.ProfileModule"/>
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</httpModules>
</system.web>
For creating Custom HTTPModule we have to implement System.Web.IHttpModule Interface.
Metadata for IHTTPModule looks like this,
using System;
namespace System.Web
{
public interface IHttpModule
{
void Dispose();
void Init(HttpApplication context);
}
}
We Can start with creating a class HTTPModuleClass and inherit IHTTPModule.Create an event handler,
public event EventHandler BeginRequest;
As we saw IHTTPModule have
- Dispose()
- Init(HttpApplication context);
Implement Init in our Custom Class HTTPModuleClass and declare the event handler for BeginRequest which Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
}
Then define the event OnBeginRequest.
public void OnBeginRequest(object sender, EventArgs e)
{
BeginRequest(this, new EventArgs());
}
So now our class looks like this,
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace HTTPModuleClassTest
{
public class HTTPModuleClass : IHttpModule
{
public event EventHandler BeginRequest;
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
}
public void OnBeginRequest(object sender, EventArgs e)
{
BeginRequest(this, new EventArgs());
}
}
}
Now we can move to Global.asax.Import Namespace
<%@ Import Namespace="HTTPModuleClassTest" %>
and create the event HTTPModuleClass_BeginRequest as
void HTTPModuleClass_BeginRequest(object sender, EventArgs e)
{
HttpBrowserCapabilities httpBrowser = this.Request.Browser;
Response.Write("You are using " + httpBrowser.Browser);
Response.Write(" [Version: " + httpBrowser.Version + "]");
}
Finally comes into configuration section.Open web.config and add the new custom HTTPModule.
<httpModules>
<add name="HTTPModuleClass" type="HTTPModuleClassTest.HTTPModuleClass"/>
</httpModules>