HTTP Modules in Asp.Net


In Asp.Net web application, every request has to pass through pipeline model. In this Asp.Net pipeline, first request has to pass through HTTP Modules then it has to pass through HTTP handlers. HTTP module can examine the request, can act on the request immediately, can change the request, can return error or can throw the exception. If HTTP module returns error or throws the exception, the request processing further will get stop and returns error to user.

There are many number of predefined HTTP modules which process the Asp.net request. We can create custom HTTP module to process the request based on requirements. We can register this custom HTTP Module in application Web.config to make it available only within the application or register it in .Net Framework Web.config file located at C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config to make it available for all applications within the machine.

Open Microsoft Visual Studio => Create empty Asp.Net Web application and add class, name it as SimpleHttpModule. Implements IHttpModule interface to create the custom HTTP Module. Implement the IHttpModule methods Init() and Dispose() methods as shown below.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

 

namespace HTTPModuleExp

{

    public class SimpleHttpModule: IHttpModule

    {

        public void Dispose()

        {

           

        }

 

        public void Init(HttpApplication context)

        {

            context.EndRequest += EndRequest;

        }

 

        private void EndRequest(object sender, EventArgs e)

        {

            if (HttpContext.Current.Handler != null)

            {

                Page objPage = HttpContext.Current.Handler as Page;

               

                if (objPage != null)

                {

                    HttpContext.Current.Response.Write("Your IP Address: " + HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] + " ; Server Time: " + DateTime.Now.ToString());

                }

            }

        }

    }

}


As shown above in Init() method we added the method for HttpApplication EndRequest. Here we implemented to display current user system IP address and server time on each page. As we know HTTP module executes for each request including javascript, css requests, image requests,….etc, we want to execute our custom http module only for Asp.Net web page. So we filtered the requests for Asp.Net page by converting the eqch request to System.Web.UI.Page class. If resultant value is not null, the request is for Asp.Net page, otherwise request is for something else.

Now build the application and register this HTTP Module in application Web.config file or .Net Framework Web.config file under System.webServer tag as shown below.

  <system.webServer>

    <modules>

      <add name="httpModule1" type="HTTPModuleExp.SimpleHttpModule, HTTPModuleExp"/>

    </modules>   

  </system.webServer>

Let’s add Asp.Net web page to the application and browse the application. You can find system IP address and server time on each page.

                                                                                                                              HTTPModuleExp.zip (22.5KB)