
Understanding the Role of the Global.asax File
Global.asax is an optional Web file that allows the Web developer to have code run based on certain events.
With .NET 2.0 and newer, Global.asax files do not have a designer surface or a code-behind file. You can add a
Global.asax file using the Add New Item dialog box.
This file defines a server side <script> block that includes a set of event handlers you may wish to program
against. Unlike an *.aspx file, Global.asax will never define control markup or ASP.NET control definitions.
Rather, this file contains a “canned” number of predefined event handlers which are understood by ASP.NET.
Initially, your code file looks like the following (shown in C# code, VB code would be identical, but in the
syntax of VB).
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{ // Code that runs on application startup }
void Application_End(object sender, EventArgs e)
{ // Code that runs on application shutdown }
void Application_Error(object sender, EventArgs e)
{ // Code that runs when an unhandled error occurs }
void Session_Start(object sender, EventArgs e)
{ // Code that runs when a new session is started }
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event
// is raised only when the sessionstate mode
// is set to InProc in the Web.config file.
// If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
In reality, this <script> block is a class named Global that derives from HttpApplication. HttpApplication
represents your Web application as a whole. This class defines a number of members, many of which are
identical to the members you inherit from Page (Request, Response, Session, etc).
Here are some of the interesting members of the HttpApplication base class.
This class also defines a set of events; many of which are handled in your Global.asax file. The following table
documents the role of each event handler, and the sort of code commonly authored within their scopes.
HttpApplication defines many additional events beyond the set automatically accounted for in Global.asax
(see the .NET Framework SDK 2.0 documentation for full details).
Global.asax File
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
HttpApplication Properties
|
Meaning of Life
|
Application
|
Gets the state (a collection of all files, pages, handlers, modules, and code) for the current request, represented by HttpApplicationState object.
|
Context
|
Gets HTTP-specific information about the current instance of the application, represented by HttpContext object. Not to be confused with classic ASP’s ObjectContext (used for transactions).
|
Request
|
Gets the intrinsic request object for the current request (HttpRequest)
|
Response
|
Gets the intrinsic response object for the current request (HttpResponse)
|
Server
|
Gets the intrinsic server object for the current request (HttpServerUtility).
|
Session
|
Gets the intrinsic session object that provides access to session data (HttpSessionState).
|
|
Global.asax Event Hander
|
Meaning in Life
|
Application_Start()
|
Fired when the Web application is launched and the AppDomain is created. This is an ideal place to connect to the database and retrieve static information used throughout the Web application.
|
Application_End()
|
Fired when the application is shutting down and the AppDomain is being destroyed. Clean up any resources obtained in the Application_Start() event.
|
Session_Start()
Session_End
|
These events are fired when a user’s Web session begins or ends.
|
Application_Error()
|
This is a global error handler. It is fired when the Web application throws an unhanded exception. Outside of a custom Http Module, it is the last chance for your Web application to handle an error before the user is notified.
|
|
Services