Session and Application Startup and Shutdown global.asax Compiles to a Class

304 If the cookie has subkeys, the Values property is an instance of NameValuesCollection defined in the System.Collections.Specialized namespace. This collection holds the subkey namevalue pairs. On subsequent requests, the cookies are available for reading from the Cookies collection of the ASP.NET Request object available through the Request property of the Page class. For example: Dim cookie As HttpCookie = Request.CookiesMyCookie Label1.Text = cookie.Value

6.9 Application-Level Code and global.asax

All of the code shown so far has been written at the page level, appearing either in an .aspx file or in a code-behind file that implements a Page-derived class. However, there needs to be a way to handle events and manipulate properties at the application level as well, without regard to any particular page. For this purpose there is the global.asax file. global.asax is a file that optionally can appear in a web applications root directory. If it is present, it can contain code and settings that are automatically processed by the ASP.NET framework at the appropriate times.

6.9.1 Session and Application Startup and Shutdown

Some applications may need to run certain code whenever a new session is started or is about to end, or when the application as a whole is first started or is about to end. This is done by placing code in the applications global.asax file, as shown in Ex am ple 6- 9 . Example 6-9. Responding to session and application start and end in a global.asax file script language=vb runat=server Public Sub Session_OnStart ... End Sub Public Sub Session_OnEnd ... End Sub Public Sub Application_OnStart ... End Sub Public Sub Application_OnEnd ... End Sub script Notice that code is placed in script blocks. The code in Ex am ple 6- 9 defines four subroutines: Session_OnStart Called by the ASP.NET framework when a session starts Session_OnEnd 305 Called by the ASP.NET framework when a session ends Application_OnStart Called by the ASP.NET framework when an application starts that is, when the applications first session starts Application_OnEnd Called by the ASP.NET framework when an application ends that is, when the applications last remaining session ends These four subroutines arent event handlers—they are simply subroutines that are automatically executed when present. They are typically used to set up and dispose of information needed at the session or application level. For example, an online shopping application could use the Application_OnStart subroutine to read popular product information from a database and cache it in the Application object, thereby improving performance for users of the application.

6.9.2 global.asax Compiles to a Class

The first time an application is accessed, its global.asax file is compiled into a class that inherits from the HttpApplication class defined in the System.Web namespace. The subroutines declared in global.asax become methods of the compiled class. Although the members of the HttpApplication class are not discussed further in this book, be aware that, if desired, those members can be accessed by code in global.asax. In addition, events exposed by the HttpApplication class can be handled by adding event handlers to global.asax. Each event handler must have the right signature, as defined by that events documentation and a Handles MyBase.EventName clause to register the handler with the event. Again, the events exposed by the HttpApplication class arent discussed further in this book. However, in case you would like to pursue it on your own, Ex am ple 6- 10 shows how such an event handler can be declared in global.asax. Example 6-10. Handling an HttpApplication class event in global.asax script language=vb runat=server ... Handle the base classs BeginRequest event. The name of the event handler is not important, but the signature and the Handles clause are. Private Sub HttpApplication_BeginRequest _ ByVal sender As Object, _ ByVal e As EventArgs _ Handles MyBase.BeginRequest ... End Sub script

6.9.3 Adding Global Objects