Categories

Friday, May 16, 2008

The Global.asax, which also known as ASP .NET application file, is located in the root directory of an ASP .NET application. This file contains codes that are executed in response to application-level and session-level events rose by ASP .NET or by HTTP modules.

There are a few things that needs to be take note about Global.asax, as below: The Global.asax is an optional file. You can remove the file if it is unnecessary. Any direct URL request to retrieve this file will be rejected automatically. External users are restricted from download or view the code written within it. If there are any changes on an active Global.asax file, the ASP .NET page framework will firstly complete all current requests for the application, sends the Application_End event to any listeners, and then restarts the application domain. In effect, this reboots the application, closing all browser sessions and flushing all state information. When the next incoming request from a browser arrives, the ASP.NET page framework re-parses and recompiles the Global.asax file and raises the Application_Start event.

In Visual Studio 2005, the Global.asax is not created by default. To create a Global.asax in your web project, right click your web site or web project in your solution explorer, choose Add New Item from the context menu. From the Visual Studio Installed Template, select Global Application Class and you can see something similar to the figure below:

Application and Session Events in Global.asax

Application Handlers:Application_Start – invoked when the application first startsApplication_Init – invoked after Application_Start and is used for initializing codeApplication_Disposed – invoked before destroying an instance of an applicationApplication_Error – invoked when an exception occursApplication_End – invoked when the application ends and used to clean up variables and memory

Request Handlers:Application_BeginRequest – invoked when an user makes a request for the applicationApplication_EndRequest – invoked at the end of each requestApplication_PreRequestHandlerExecute – invoked before ASP .NET executes an event handlerApplication_PostRequestHandlerExecute – invoked after ASP .NET handler has finished its executionApplication_PreSendRequestHeaders – invoked before ASP .NET sends HTTP header to the clientApplication_PreSendRequestContent – invoked before ASP .NET sends contents to the clientApplication_AuthenticateRequest – invoked before a user credentials are authenticatedApplication_AuthorizeRequest – invoked on successful authentication of a user credentials. You can create your own user authorization to access the resources of an applicationApplication_ResolveRequestCache – invoked on successful completion of the authorization requestApplication_AcquireRequestState – invoked when ASP .NET acquires the current state associated with the current requestApplication_ReleaseRequestState – invoked before current state data in the session collection is serializedApplication_UpdateRequestCache – invoked before information is added to output cache of the page

Session Methods:Session_Start – invoked when session starts on each user who requesting a pageSession_End – invoked when the session of a user times out or ends