ASP.NET offers two methods for extending standard processing behavior: HTTP modules and HTTP handlers.

HTTP Modules

An HTTP module is an assembly that is called on every request made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life cycle events throughout the request (source).

There are two steps required to create custom HTTP module:

  1. Create class which implements [cci]IHTTPModule[/cci] interface
  2. Register your module in web.config
public interface IHttpModule
{
  void Init(HttpApplication context);
  void Dispose();
}

Each time a request is made to your application, [cci]Init[/cci] method is being called. Having a reference to HTTPApplication you can subscribe to the application's event, like BeginRequest and EndRequest.

public class HelloWorldModule : IHttpModule
{
  public void Init(HttpApplication application)
  {
    application.BeginRequest += this.Application_BeginRequest;
    application.EndRequest += this.Application_EndRequest;
  }

  private void Application_BeginRequest(Object source, EventArgs e)
  {
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;
    context.Response.Write("HelloWorldModule: Beginning of Request");
  }

  private void Application_EndRequest(Object source, EventArgs e)
  {
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;
    context.Response.Write("HelloWorldModule: End of Request");
  }

  public void Dispose() {}
}

To register, you need to add the following in [cci]Web.config[/cci]:

<configuration>
  <system.web>
    <httpModules>
      <add name="HelloWorldModule" type="HelloWorldModule"/>
    </httpModules>
  </system.web>
</configuration>

This module will cause each page returned from the server to be decorated with a header and a footer.

ASP.NET provides built-in modules, which are a part of standard processing. There modules are listed in the framework's [cci]Web.config[/cci] placed in [cci]c:\Windows\Microsoft.NET\Framework\v4.0.30319\[/cci] folder.

<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=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
  <add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

[cci]HTTPApplication[/cci] class has a property [cci]Modules[/cci] which contains all the loaded modules. To see them just set a breakpoint inside [cci]Global.asax[/cci] and investigate Watch window.

HTTP Handlers

An ASP.NET HTTP handler is the process that runs in response to a request made to an ASP.NET Web application. It is slightly different than a module. Handlers are sometimes referred to as "endpoints". A handler can be associated with file extension. E.g. file extension [cci].aspx[/cci] is assigned to [cci]System.Web.UI.PageHandlerFactory[/cci] class, which handles standard ASP.NET page processing. In this Stack Overflow question there is a good two-sentence explanation of the difference between HTTP Module and HTTP Handler: HttpHandler is where the request train is headed. HttpModule is a station along the way. So when we request ASPX page all the registered modules will be initialized and only one HTTP handler will be run.

Creating custom HTTP handler is easy as it was with HTTP modules. You should implement [cci]IHttpHandler[/cci] interface and register handler in [cci]Web.config[/cci] class.

To see which HTTP handler is currently being run in the current request let's see standard ASP.NET and ASP.NET MVC application. We need to set a breakpoint in code-behind class for the first and in a controller for the second. There result are as below.

The second of these is [cci]MvcHandler[/cci], but the first shows that our handler is actually our ASP.NET page. Let's look at [cci]WebForm1[/cci] class. It inherits from [cci]System.Web.UI.Page[/cci] which inherits from [cci]TemplateControl[/cci] and implements [cci]IHttpHandler[/cci] interface. It means that each ASP.NET page is actually a HTTP handler itself. But how the process know about our handler, while the framework's [cci]Web.config[/cci] file contains:

<add path="*.aspx" verb="*"
     type="System.Web.UI.PageHandlerFactory"
     validate="True" />

The [cci]PageHandlerFactory[/cci] class creates an instance of our [cci]WebForm1[/cci] class based on the request's URL.