Using Modules with the Global.asax File

ASP.NET provides several modules that participate in each request and expose events you can handle in Global.asax. You can customize and extend these modules as you like, or develop completely new custom modules to process information for and about HTTP requests made to your ASP.NET-based application. For example, you could create an output cache module that implements output-caching behaviors for your entire application.

All modules, whether custom or provided by the .NET Framework, must implement the IHttpModule interface. As long as these modules are registered with your application, you can easily interact with the HTTP requests coming in to your application.

Handling HttpModule Events

You can use the Global.asax file to handle any event exposed by the modules in the request. For example, you might create a custom authentication module for your ASP.NET Web application in which you might expose an OnAuthenticateRequest event. The code that you write to handle the events exposed by an HttpModule must conform to the following naming pattern:

FriendlyModuleName_EventName(AppropriateEventArgumentSignature)

For example, if you want to include event-handling code for the beginning and end of a session, as well as for an OnAuthenticateRequest event, it could look like the following.

<Script language="VB" runat="server">
     Sub Session_OnStart()
         'Session start-up code goes here.
     End Sub
     Sub Session_OnEnd()
         'Session clean-up code goes here.
     End Sub
     Sub Application_OnAuthenticateRequest(Source As Object, Details as EventArgs)
         'Authentication code goes here.
     End Sub
</script>
[C#]
<Script language="C#" runat="server">
     void Session_OnStart() {
         // Session start-up code goes here.
     }
     void Session_OnEnd() {
         // Session clean-up code goes here.
     }
     void Application_OnAuthenticateRequest(Object Source, EventArgs Details) {
         // Authentication code goes here.
     }
</script>

See Also

ASP.NET Applications | IHttpModule