Updated April 18, 2023
Introduction to Global.asax in Asp.net
Global.asax is an optional files used to declare and handle the application and session-level events and objects. Global.asax is the asp.net extension of the asp global.asax file. The global asax file resides in the IIS virtual root of an asp.net application. 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 on download the code in it. Code to handle the application events 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. Global.asax is used to declare data that is available across different application request sent across different browsers sessions. This process is known as application and session state management. Let us discuss the Global.asax in Asp.net.
Types of Events of Global.asax
There are two types of events in global.asax file in your application:
- Events which get called for every request.
- Events only get invoked under certain conditions.
First Type of Event
For the first type of event which occurs for every request, they get executed in the following order:
- Application_BeginRequest(): This is called at the start of every request.
- Application_AuthenticateRequest(): This is called before authentication is performed.
- Application_AuthorizeRequest(): This is called when the authentication stage is completed.
- Application_ResolveRequestCache(): This is often used in conjunction with output caching.
- Then the request is sent to its assigned handler.
- Application_AcquirrRequestState(): This is called just before session-specific data is retrieved for the client and is used to populate session collection.
- Application_PreRequestHandlerExecute(): This is called before the appropriate HTTP handler executes the request.
- The handler now executes the request. The code of your asp.net web form large is now executed and the HTML is now rendered.
- Application_PostRequestHandlerExecute(): This is called just after the request is handled by its appropriate HTTP handler
- Application_ReleaseRequestState(): This is called when the session-specific information is about to be serialized from the session collection so that its available for the next request.
- Application_UpdateRequestCache(): This is called just before the information is added to the output cache
- Application_EndRequest(): This is called at the end of the request right before the objects are released and reclaimed. It’s the place to add your cleanup code.
Second Type of Event
For the second type of application events that do not fire upon every request, there are six of them:
- Application_Start(): This is called when the application first starts up and the application domain is created. This is place for you to insert application wide initialization code.
- Session_Start(): This method is invoked each time a new session begins. This is often used to initialize user specific information. Some developers also use this to count the number of current active user session in the application.
- Application_Error(): This is invoked whenever an unhandled exception occurs in the application. You may want to use this to perform some custom error handling logic application wide.
- Session_End(): This is called when user session ends. A session ends when your code releases it explicitly or its idle time exceeds the set expiry timeout. Developers add the code here to clean up any related data.
- Application_End(): This method is invoked just before an application ends. the end of an application can occur because IIS is shut down or the application is transitioning to anew domain in response to file updates or process recycling.
- Application_Disposed(): This method is called sometime after the application is shut down and the garbage collector is about to reclaim the memory it occupies. Note that it is already too late to perform critical cleanup at this point of time but you may want to add code here to verify that resources have been released as last which is safe proof
Global.asax file must reside on the IIS virtual root. remember that a virtual root can be thought of as the container of a web application. Events and states specified in the global file are then applied to all resources housed within the web application. For example, global.asax defines a state application variable, all .asax files within the virtual root will be able to access the variable.
The Asp.net global.asax file can coexist with asp global.asax file. You can create a global.asax file either in WYSIWYG designer or as a compiled class that you deploy on your applications bin directory as an assembly. However, in the latter case, you need global.asax file that refers the assembly.
Conclusion – Global.asax in Asp.net
Here we have discussed the basic concepts of global.asax file in asp.net. We have also discussed the events that occurred in global.asax. Hope you enjoyed the article.
Recommended Articles
This is a guide to Global.asax in Asp.net. Here we discuss the Introduction of Global.asax in Asp.net and its types of events in detail. You can also go through our other suggested articles to learn more –