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
Create a new class implementing
IHttpModule
Override
Init
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
Write a handler (within the module) for every event you want to handle
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
Select
Web site | Add New Item
Select
Global Application Template
from the templatesInsert 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