In this article we are going to see the importance or use of global.asax file in Asp.Net 2.0, Asp.Net 3.0 and Asp.Net 3.5.
In Asp.net 1.1 when you create web application with visual studio 2003 editor it will automatically create global.asax file, but this is not the same case with visual studio 2005, 2008 it will not create global.asax file by default. You need to explicitly add global.asax file to make use of this.
But here I want to make it clear that all the event and method will fired according to it nature.
So till here we are clear that in visual studio 2005 and 2008 global.asax file is default and if you want to add it you have to add manually.
How to create or add global.asax file in Asp.Net 2.0, 3.0 and 3.5?
Its simple , Right click on the solution explorer in visual studio editor -> Add New Item -> Select "Global Application Class" - > press "Add" Button
Now you have added global.asax file in you web application.
By default you can see that below event method is automatically available in your global.asax file.
<%@ Application Language="C#" %>
Now we will explore each above listed method and its importance one by one.
Application_Start - This method will get fired when First time the application and web server (eg. IIS ) starts, That means If the IIS recycle or restart again then this event also will be fired.
Application_End - This method will be fired when When application started shut down.
Application_Error - This event will be fired when there is any exception occurred in the application during run time. This event method is one of most important method used by most of the web developwer.
Session_Start - This event will be fired when a new user request a page from the server. That means for every user request the page this event will b fired.
Session_End - This code will be fired when session end for a use. But make sure this event willl only fired when Session Mode is "InProc" as we know there are three session state InProc, StateServer, SqlServer mode to store session. For more information on Session in Asp.net Please Refer this Tutorial
Now let's see real time example of the above events present in global.asax file.
Create Hits counter of web application.
We are going to make use of Application_Start event and Session_Start even to achieve hit counter.
public void Application_Start(object sender, EventArgs e)
//Create Application object and assign 0 to it
Application["TotalHits"] = 0;
public void Session_Start(object sender, EventArgs e)
//Lock the Application using Lock method of application
//Increment by 1 Application object every time when session starts
Application["TotalHits"] = Application["TotalHits"] + 1;
//Unlock the Application Object using Unlock() method if Application object
In the above example Application["TotalHits"] Holds the total number of count of visitor.
Another realtime example of global.asax file
Catch error in application level .
void Application_Error(object sender, EventArgs e)
Exception exception = Server.GetLastError();
string message = "Error occurred in - " + Request.Url.ToString() + "and error message is - "
+ exception.Message + " Stack Trace - " + exception.StackTrace.ToString();
Hope this will help developer to understand the importance or use of Global.asax file in Asp.Net application.