.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Importance or use of global.asax file

Posted By:Pankaj Mishra       Posted Date: January 17, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

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.



Like This

<%@ 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.


 Subscribe to Articles


Further Readings:

Author: Karode         Company URL: http://www.dotnetspark.com
Posted Date: September 14, 2010

Hi Pankaj,

I really like this article. I was looking for some good articles on global.asax file . I found your article very helpful. I tried to apply in my own small application. It is working fine on my internet explorer. But one thing is not clear why this is not working on mozilla . i need your help regarding this.
Thanks a lot in advance.

Gaurav karode

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend