Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Custom HTTP Module

0.00/5 (No votes)
13 Sep 2009 1  
Before starting have a look in the Web.config in the locationC:WindowsMicrosoft.NETFrameworkversionnumberCONFIGWeb.configYou can see the

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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
{
    // Summary:
    //     Provides module initialization and disposal events to the implementing class.
    public interface IHttpModule
    {
        // Summary:
        //     Disposes of the resources (other than memory) used by the module that implements
        //     System.Web.IHttpModule.
        void Dispose();
        //
        // Summary:
        //     Initializes a module and prepares it to handle requests.
        //
        // Parameters:
        //   context:
        //     An System.Web.HttpApplication that provides access to the methods, properties,
        //     and events common to all application objects within an ASP.NET application
        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;

/// <summary>
/// Summary description for HTTPModuleClass
/// </summary>
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>

Build and Run the application.

 Download Sample Application.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here