ASP.NET Application Phases
 
Global.asax in ASP.NET 2.0
 
Global.asax is a file that exists in the root directory of your application. It is not accessible over the web but is used by the ASP.NET application if it is there. It is a set of event handlers that you can use to change and set settings in your site. The events can come from one of two places - The HTTPApplication object and any HTTPModule object that is specified in web.confg or machine.config. We will be covering both here.
 
Global.asax Events
 
Global.asax includes a lot useful events to work with your application for customization. The Global.asax file contains the following events:
 
• Application_Init: This event is raised when an application initializes or is first called. It's invoked for all HttpApplication object instances.

• Application_Disposed: This event is raised just before an application is destroyed. This is the ideal location for cleaning up previously used resources.

• Application_Error: This event is raised when an unhandled exception is encountered within the application.

• Application_Start: This event is raised when the first instance of the HttpApplication class is created. It allows you to create objects that are accessible by all HttpApplication instances.

• Application_End: This event is raised when the last instance of an HttpApplication class is destroyed. It's fired only once during an application's lifetime.

• Application_BeginRequest: This event is raised when an application request is received. It's the first event fired for a request, which is often a page request (URL) that a user enters.

• Application_EndRequest: This is the last event fired for an application request.

• Application_PreRequestHandlerExecute: This event is raised before the ASP.NET page framework begins executing an event handler like a page or Web service.

• Application_PostRequestHandlerExecute: This event is raised when the ASP.NET page framework is finished executing an event handler.

• Applcation_PreSendRequestHeaders: This event is raised before the ASP.NET page framework sends HTTP headers to a requesting client (browser).

• Application_PreSendContent: This event is raised before the ASP.NET page framework sends content to a requesting client (browser).

• Application_AcquireRequestState: This event is raised when the ASP.NET page framework gets the current state (Session state) related to the current request.

• Application_ReleaseRequestState: This event is raised when the ASP.NET page framework completes execution of all event handlers. This results in all state modules to save their current state data.

• Application_ResolveRequestCache: This event is raised when the ASP.NET page framework completes an authorization request. It allows caching modules to serve the request from the cache, thus bypassing handler execution.

• Application_UpdateRequestCache: This event is raised when the ASP.NET page framework completes handler execution to allow caching modules to store responses to be used to handle subsequent requests.

• Application_AuthenticateRequest: This event is raised when the security module has established the current user's identity as valid. At this point, the user's credentials have been validated.

• Application_AuthorizeRequest: This event is raised when the security module has verified that a user can access resources.

• Session_Start: This event is raised when a new user visits the application Web site.

• Session_End: This event is raised when a user's session times out, ends, or they leave the application Web site.
 
Exposed Events
 
The HTTPApplication class exposes the following events for you to handle.
 
• AcquireRequestState

• AuthenticateRequest

• AuthorizeRequest

• BeginRequest

• EndRequest

• Error

• PostRequestHandlerExecute

• PreRequestHandlerExecute

• PreSendRequestContent

• PreSendRequestHeaders

• ReleaseRequestState

• ResolveRequestCache

• UpdateRequestCache
 
Remember that it will automatically search for an event handler like - Application_EventName(sender as object, e as EventArgs)
 
Handling the Events
 
Let's have a look towards a sample Global.asax file -
 
<%@ Import Namespace="System.Diagnostics" %>

<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Started!", EventLogEntryType.Information)
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Session.Contents.Add("TimeStart", DateTime.Now())
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Error Occured!", EventLogEntryType.Error)
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
EventLog.WriteEntry("Sample Application", "Application Ended!", EventLogEntryType.Warning)
End Sub
</script>
 
As you can see the above example handles four events.
 
ASP.NET will automatically compile and process this file. The Application_Init and Application_Start events are fired once when the application is first started. Likewise, the Application_Disposed and Application_End are only fired once when the application terminates. In addition, the session-based events (Session_Start and Session_End) are only used when users enter and leave the site. Notice that it looks almost like a normal ASP.NET page, but without the HTML content.