Global.asax
This article needs additional citations for verification. Please help improve this article by adding reliable references. Unsourced material may be challenged and removed. (July 2009) |
Introduction
The Global.asax file is an optional file used to declare and 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
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.[1]
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 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 deploy 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 [2].
- 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 application terminates.
- 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).
For a complete list of Global.asax events see "Global.asax Events".
References
- ↑ MSDN: Global.asax Syntax
- ↑ Joydip Kanjilal, "Understanding the Global.asax file: ASP Alliance", ASP Alliance. Accessed on July 20, 2009
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...