Every ASP.NET application, by default, gets a Global.asax file. This file contains six methods that the ASP runtime calls when appropriate. The methods include Application_Start() , Session_Start() , Application_BeginRequest() , Application_EndRequest() , Session_End() , and Application_End() . The most commonly used of these are the Application_Start() and the Session_Start() methods. These are especially valuable because the Application_Start() method can store into application variables any data that the application will use while it is alive . The Session_Start() method can create and set up any kind of session variables that are needed for a user 's session. This means that your job of maintaining state is easier because you have guaranteed points where sessions and applications start and end. The code in Listing 17.1 shows a C# Global.asax file, and the code in Listing 17.2 shows the default code for a VB Global.asax file. This code shows a Visual Studio-created Global.asax file and the methods that it makes available by default. Listing 17.1 The Default Code for a C# Global.asax FileC# Global.asax protected void Application_Start(Object sender, EventArgs e) { } protected void Session_Start(Object sender, EventArgs e) { } protected void Application_BeginRequest(Object sender, EventArgs e) { } protected void Application_EndRequest(Object sender, EventArgs e) { } protected void Session_End(Object sender, EventArgs e) { } protected void Application_End(Object sender, EventArgs e) { } Listing 17.2 The Default Code for a VB Global.asax File'VB Global.asax Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the application is started End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the session is started End Sub Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) ' Fires at the beginning of each request End Sub Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As [ic:ccc]EventArgs) ' Fires upon attempting to authenticate the user End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Fires when an error occurs End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the session ends End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the application ends End Sub |