Global.asax

The Global.asax file is an optional file used to handle application and session-level events and objects for an ASP.NET web site running on an IIS Web Server. The file contains ASP.NET program code, and is the .NET counterpart of the Global.asa file used for ASP. The Global.asax file resides in the IIS virtual root of an ASP.NET application.
How it works
Effectively, global.asax allows you to write code that runs in response to "system level" events, such as the application starting, a session ending, an application error occurring, without having to try and shoe-horn that code into each and every page of your site.
At run time, upon the arrival of the first request, Global.asax is parsed and compiled into a dynamically generated .NET Framework class. ASP.NET is configured so that any direct request for the Global.asax is automatically rejected; external users cannot view or download the code in it.
Code to handle application events (such as the start and end of an application) resides in Global.asax. Such event code cannot reside in the ASP.NET page or web service code itself, since during the start or end of the application, its code has not yet been loaded (or unloaded). Global.asax is also used to declare data that is available across different application requests or across different browser sessions. This process is known as application and session state management.
The Global.asax file, also called as the ASP.NET application file.The Global.asax file must reside in the IIS virtual root. A virtual root can be thought of as the container of a web application. Events and state specified in the global file are then applied to all resources housed within the web application. If, for example, Global.asax defines a state application variable, all .aspx files within the virtual root will be able to access the variable.
The ASP.NET Global.asax file can coexist with the ASP Global.asa file. A Global.asax file is created in either a WYSIWYG designer or as a compiled class that is deployed in an application's \Bin directory as an assembly. However, in the latter case, the Global.asax file must refer to the assembly.
Like an ASP.NET page, the Global.asax file is compiled upon the arrival of the first request for any resource in the application. The similarity continues when changes are made to the Global.asax file: ASP.NET automatically notices the changes, recompiles the file, and directs all new requests to the newest "compilation".
Events in the Global.asax file
The following are important events catered for in the Global.asax file:
* Application_Init: Fires when the application initializes for the first time.
* Application_Start: Fires the first time an application starts.
* Session_Start: Fires the first time when a user’s session is started.
* Application_BeginRequest: Fires each time a new request comes in.
* Application_EndRequest: Fires when the request ends.
* Application_AuthenticateRequest: Indicates that a request is ready to be authenticated.
* Application_Error: Fires when an unhandled error occurs within the application.
* Session_End: Fires whenever a single user Session ends or times out.
* Application_End: Fires when the application ends or times out (Typically used for application cleanup logic).
 
< Prev   Next >