What is Global.asax file: global.asax allows us to write event handlers that react to global events in web applications.
How it gets called: Global.asax files are never called directly by the user, rather they are called automatically in response to application events.
Point to remember about global.asax:
- They do not contain any HTML or ASP.NET tags.
- Contain methods with specific predefined names.
- They defined methods for a single class, application class.
- They are optional, but a web application has no more than one global.asax file.
How to add global.asax file:
Select Website >>Add New Item (or Project >> Add New Item if you're using the Visual Studio web project model) and choose the Global Application Class template.

After you have added the global.asax file, you will find that Visual Studio has added Application Event handlers:
- <%@ Application Language="C#" %>
- <script runat="server">
- void Application_Start(object sender, EventArgs e)
- {
- // Code that runs on application startup
- }
- void Application_End(object sender, EventArgs e)
- {
- // Code that runs on application shutdown
- }
- void Application_Error(object sender, EventArgs e)
- {
- // Code that runs when an unhandled error occurs
- }
- void Session_Start(object sender, EventArgs e)
- {
- // Code that runs when a new session is started
- }
- void Session_End(object sender, EventArgs e)
- {
- // Code that runs when a session ends.
- // Note: The Session_End event is raised only when the sessionstate mode
- // is set to InProc in the Web.config file. If session mode is set to StateServer
- // or SQLServer, the event is not raised.
- }
- </script>
Purpose of these application event handlers: To reach and handle any HttpApplication event.
If you want to know about HttpApplication events, see the MSDN article.
How these handler reach to HttpApplication Events
- global.asax file aren't attached in the same way as the event handlers for ordinary control events
- Way to attach them is to use the recognized method name, i.e. for event handler Application_OnEndRequest(), ASP.NET automatically calls this method when the HttpApplication.EndRequest event occurs.
What kind of application event global.asax can handle:
Global.asax can handle 2 types of applications events
1) Events that occurs only under specific conditions, i.e. request / response related events.
Order in which application event handler executes;
| Order | Application Events | What they do |
| 1 | Application_BeginRequest() | This method is called at the start of every request. |
| 2 | Application_AuthenticateRequest() | This method is called just before authentication is performed. This is time and place where we can write our own authentication codes. |
| 3 | Application_AuthorizeRequest() | After the user is authenticated (identified), it's time to determine the user's permissions. Here we can assign user with special privileges |
| 4 | Application_ResolveRequestCache() | This method is commonly used in conjunction with output caching. |
| 5 | Application_AcquireRequestState() | This method is called just before session-specific information is retrieved for the client and used to populate the Session collection. |
| 6 | Application_PreRequestHandlerExecute() | This method is called before the appropriate HTTP handler executes the request. |
| 7 | Application_PostRequestHandlerExecute() | This method is called just after the request is handled. |
| 8 | Application_ReleaseRequestState() | This method is called when the session-specific information is about to be serialized from the Session collection so that it's available for the next request. |
| 9 | Application_UpdateRequestCache() | This method is called just before information is added to the output cache. |
| 10 | Application_EndRequest() | This method is called at the end of the request, just before the objects are released and reclaimed. It's a suitable point for cleanup code. |

Hitanshi MehtaPosted Sep 30, 2018, 3:23 PM
Nice article!!!
veera muthuPosted Jun 23, 2016, 1:39 AM
Very good.....
MiraclePosted Sep 19, 2013, 6:58 AM
nice article by the way, anyway I want to as k is it possible to edit this file (global.asax) without compiled? thank you
manav jhaPosted Mar 31, 2011, 1:33 AM
very gr8 article. i hope we'll get more article in couple of weeks. keep good writing..vishal. god bless u.