How can we use global.asax in asp.net? And what is that?
6 Answers
MSDN has an outline of the purpose of the global.asax file.
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 occuring, without having to try and shoe-horn that code into each and every page of your site.
You can use it by by choosing Add > New Item > Global Application Class in Visual Studio. Once you've added the file, you can add code under any of the events that are listed (and created by default, at least in Visual Studio 2008):
- Application_Start
- Application_End
- Session_Start
- Session_End
- Application_BeginRequest
- Application_AuthenticateRequest
- Application_Error
There are other events that you can also hook into, such as "LogRequest".

- 45,296
- 24
- 122
- 150
-
5The entry for global.asax has been deleted from Wikipedia. – Cathy Sullivan Aug 06 '15 at 22:27
-
Can we add global.asax file for windows form? – Jeeva J Oct 07 '15 at 05:13
-
1@JeevaJsb, no. The contents of global.asax is (generally) events that are triggered by the IIS/ASP.net application lifecycle so there would be nothing to trigger the code in a WinForms application. – Rob Oct 07 '15 at 07:38
-
1I wonder why they can't have just a code inheriting from HttpApplication class and not requiring the asax file. The global.asax.cs contains all the stuff so why the need of a specific asax file that only contains this <%@ Application Codebehind="Global.asax.cs" Inherits="XXXXX" Language="C#" %> – Simple Fellow Oct 31 '17 at 07:36
Global asax events explained
Application_Init: Fired when an application initializes or is first called. It's invoked for all HttpApplication object instances.
Application_Disposed: Fired just before an application is destroyed. This is the ideal location for cleaning up previously used resources.
Application_Error: Fired when an unhandled exception is encountered within the application.
Application_Start: Fired 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: Fired when the last instance of an HttpApplication class is destroyed. It's fired only once during an application's lifetime.
Application_BeginRequest: Fired 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: The last event fired for an application request.
Application_PreRequestHandlerExecute: Fired before the ASP.NET page framework begins executing an event handler like a page or Web service.
Application_PostRequestHandlerExecute: Fired when the ASP.NET page framework is finished executing an event handler.
Applcation_PreSendRequestHeaders: Fired before the ASP.NET page framework sends HTTP headers to a requesting client (browser).
Application_PreSendContent: Fired before the ASP.NET page framework sends content to a requesting client (browser).
Application_AcquireRequestState: Fired when the ASP.NET page framework gets the current state (Session state) related to the current request.
Application_ReleaseRequestState: Fired 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: Fired 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: Fired 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: Fired 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: Fired when the security module has verified that a user can access resources.
Session_Start: Fired when a new user visits the application Web site.
Session_End: Fired when a user's session times out, ends, or they leave the application Web site.

- 2,169
- 3
- 33
- 55
-
1There is also Application_Dispose ... Which I believe is linked to every Application Init ... It gets called fairy frequently.Whereas Application_End only gets called once, so I think Application_End the best place to dispose of DI containers etc. https://learn.microsoft.com/en-us/previous-versions/ms178473(v=vs.140)?redirectedfrom=MSDN – andrew pate Jan 31 '21 at 12:04
-
1Sadly, It seems if your using VS2019 IIS Express (.NET Framework 4.7.2) it doesn't call Application_End when you stop debugging. If you run your application in IIS instead you will find it does. – andrew pate Jan 31 '21 at 12:07
The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level and session-level events raised by ASP.NET or by HTTP modules.
Global.asax is the asp.net application file.
It is an optional file that handles events raised by ASP.NET or by HttpModules. Mostly used for application and session start/end events and for global error handling.
When used, it should be in the root of the website.

- 489,969
- 99
- 883
- 1,009
The root directory of a web application has a special significance and certain content can be present on in that folder. It can have a special file called as “Global.asax”. ASP.Net framework uses the content in the global.asax and creates a class at runtime which is inherited from HttpApplication. During the lifetime of an application, ASP.NET maintains a pool of Global.asax derived HttpApplication instances. When an application receives an http request, the ASP.Net page framework assigns one of these instances to process that request. That instance is responsible for managing the entire lifetime of the request it is assigned to and the instance can only be reused after the request has been completed when it is returned to the pool. The instance members in Global.asax cannot be used for sharing data across requests but static member can be. Global.asax can contain the event handlers of HttpApplication object and some other important methods which would execute at various points in a web application

- 51
- 1
- 3
The Global.asax can be used to handle events arising from the application. This link provides a good explanation: http://aspalliance.com/1114

- 995
- 1
- 8
- 18