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#" %>
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.
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.