Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

How To Create Your Own Http Module.

Introduction

The purpose this tutorial is to understand what is a Http module and how to create your own Http module. Through out this tutorial I will be using C# to explain some of the concepts.

 

Reader is assumed to know how to use of Visual Studio. Has good understanding of ASP.NET web application and last but not least know programming using .NET base class libraries framework.

What is an Http module?

So what is a Http module?

Simply put, Http module is a piece of code that sits between the client and the web server. All the request that are made through the web server passes through http module.

Lets have a look at very high level, how the request are process by ASP.NET framework. When a client makes a request to your web server the request comes in the form of Http Request. The Http Request flows through series of Http Modules. Each of the Http module have full control over the Http request. They process or modify the request. On the request passes through all the modules it is handed over to Http handler. Http handlers process the request and response passes through the all Http modules in reverse order.

ASP.NET framework already has few Http modules such as session state, authorization etc. You can find list of these modules in the machine.config file under httpModules section.

So what we can we do with Http module?

If we are creating our own Http module, we can respond to either ASP.NET events or user events in a way we want it to respond.

Authoring Http Module.

The following steps are followed while creating an Http module.

  1. Implement IHttpModule.
  2. Register the events of interest in Init method.
  3. Implement event handlers.
  4. Implement Dispose if required.
  5. Register the module in web.config.

Learn by example.

It is always easy to understand if it is explained with example. Let us follow the same practice. Let us say I have a requirement where I need to log the requests that are coming to my web application. Later on I will use this information to analyze my web site traffic.

Create RequestLoggerModule

  1. Open Visual Studio.
  2. Create New Class Library project and name it RequestLoggerExample.
  3. Delete the Class1.css that is created by default.
  4. Add a class by name RequestLoggerModule.
  5. Add reference to System.Web assembly.
  6. Import the name space System.Web.
  7. Now make RequestLoggerModule to implement IHttpModule.

At this stage RequestLoggerModule code should look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace RequestLoggerExample
{
public class RequestLoggerModule:IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
 
}
 
public void Init(HttpApplication context)
{
 
}
 
 
#endregion
}
}

The best place to log the request is the BeginRequest. In the Init method attach a event handler to BeginRequest event of the HttpApplication as shown below.

context.BeginRequest += new EventHandler(context_BeginRequest);

In the context_BeginRequest method trap the request stream and log it to a file as shown below.

void context_BeginRequest(object sender, EventArgs e)
{
    HttpApplication app = sender as HttpApplication;
    StreamWriter sw = new StreamWriter(@"C:\requestLog.txt");
    StreamReader reader = new StreamReader(app.Request.InputStream);
    sw.WriteLine(reader.ReadToEnd()); sw.Close();
}

Note that writing to a file will through exception if the user context under which ASP.NET is running do not have write access. It is not a good idea to give a write permission in your real application.

Now your code should look like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
 
namespace RequestLoggerExample
{
public class RequestLoggerModule:IHttpModule
{
#region IHttpModule Members
 
public void Dispose()
{
 
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
 
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
StreamWriter sw = new StreamWriter(@"C:\requestLog.txt");
StreamReader reader = new StreamReader(app.Request.InputStream);
sw.WriteLine(reader.ReadToEnd());
sw.Close();
}
 
#endregion
}
}

Configuring web application.

We have built the Http module successfully, let us see how we can make use of the module that we have created.

Follow the below steps to configure your existing ASP.NET we application to use the RequestLoggerModule.

  1. Copy RequestLoggerExample.dll to bin folder of your web application.
  2. Open the web.config file of you web application.
  3. Add the below shown section to your web.config under system.web node.

    <httpModules>
      <add name="RequestLoggerModule" type="RequestLoggerExample.RequestLoggerModule, RequestLoggerExample"/>
    </httpModules>

If you want this module to be used by all the application that are hosted on your server, you need to add the above section to machine.config file.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   


comments powered by Disqus