Conclusion: The Application and HTTP Module

In this tutorial, we saw how the ASP.NET architecture includes a rendezvous point for all the requests passing through an application. This is especially important in Web applications composed of disparate components centered around a pipeline. While there are certain obvious places where a request context can show up (most notably in the end handler), it's clear that there are other points in the request chain where you need to have something to hold on to.

ASP.NET offers two broad choices in implementing such a "global space." Global.asax is a convenient representation of the HttpApplication object. ASP.NET applications have a singular instance of the HttpApplication class. The application object includes a handy dictionary in which to store data that needs to survive and be available from all points within the application. In addition, Global.asax offers a place to intercept various application-wide events.

HTTP Modules offer very much the same functionality, although in a different package. HTTP Modules implement the IHttpModule interface and are deployed with the application via the Web.Config file. When an application starts up, the ASP.NET runtime looks in the Web.Config file to see if any additional handlers need to be attached to the pipeline. (ASP.NET plugs in a number of Modules already-they implement such features as authentication and session state.) When ASP.NET sees a new Module within the Web.Config file, ASP.NET loads the Module and calls the Init method. Modules usually initialize by setting up handlers for various application-wide events.

Tutorial 17 Quick Reference

How to create a custom module assembly

  1. Create a new class implementing IHttpModule

  2. Override Init

  3. Override Disposet

How to insert the module into the processing chain

Mention the module in the <httpModule> node of the application's Web.Config file

How to handle application events in the module

  1. Write a handler (within the module) for every event you want to handle

  2. During the Init method, subscribe to the events by attaching the event handlers to the events

How to override the application object in Global.asax file

  1. Select Web site | Add New Item

  2. Select Global Application Template from the templates

  3. Insert your own code for responding to the application-wide events

How to use the application's dictionary

Access the application object (it's always available from the current HttpContext). Use the indexer notation to access the dictionary