Please navigate to the bottom of the page for Table of Contents

Thursday, June 2, 2011

ASP.NET HttpModule explained

An HTTP module is a simple class that can plug themselves into the request-processing pipeline. They do this by hooking into a handful of events thrown by the application as it processes the HTTP request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.

To create an HttpModule, you simply create a class that derives from the System.Web.IHttpModule interface. This interface requires you to implement two methods: Init and Dispose. The simple code snippet below shows an HttpModule that appends a simple message at the end of every page output in your application.

using System;
using System.Web;

namespace WebApplication2
public class PageTimerModule : IHttpModule
private HttpApplication _application = null;

public void Dispose()

public void Init(System.Web.HttpApplication context)
// save the context
_application = context;
// handle the end request
context.EndRequest += new EventHandler(context_EndRequest);

// tag a message at the end of the page
void context_EndRequest(object sender, EventArgs e)
string message = string.Format("Generated at {0}",
// stream it at the end of the response

To use this module, we need to modify the web.config and add a reference to this module. This will let ASP.NET know that you want to include the module in the request processing pipeline.

<?xml version="1.0"?>

<compilation debug="true" targetFramework="4.0" />

<add name="PageTimerModule"
type="WebApplication2.PageTimerModule, WebApplication2"/>

<!-- For IIS 7, we also need to define it in the web server section-->
<add name="PageTimerModule"
type="WebApplication2.PageTimerModule, WebApplication2"/>

That’s it. HttpModules provide you with a simple and effective way to tap into the ASP.NET request pipeline. In the next post we will look into HttpHandlers.


  1. Nice explaination Nikhil. Hope to see more advanced interview questions on ASP.NET.


  2. With havin so much written content do you ever run into any problems of plagorism or copyright violation?
    My site has a lot of unique content I’ve either authored myself or outsourced but it seems a lot of it is….
    popping it up all over the internet without my agreement.
    Do you know any solutions to help prevent content from being ripped off? Cara Mengatasi Anak Susah Makan dengan Laperma Platinum

  3. It has been an absolutely fantastic business partner. niche edit backlinks work is phenomenal and the site constantly garners passionate feedback.

  4. Make sure you have the required details and/or identification and your bank card if depositing using a Debit/Visa or building society card. You’ll need to provide your email address, a username and password for logging in when signing up to your chosen casino, as well as often a short numeric code in some instances for that added level of extra security.

  5. Nice post with Great Information. I Hope you will be keep posting some articles like this. Look you spend a lot of time or effort in this article. Now it's time to avail shutters in london for more information.