Click here to Skip to main content
15,881,659 members
Articles / Event
Article

Custom HTTP Module

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 15.4K   18   1  
Before starting have a look in the Web.config in the locationC:\Windows\Microsoft.NET\Framework\versionnumber\CONFIG\Web.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.

This article was originally posted at http://wiki.asp.net/page.aspx/909/custom-http-module

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --