LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Understanding the Global.asax file
by Joydip Kanjilal
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 198305/ 311


The Global.asax file (also known as the ASP.NET application file) is an optional file that is located in the application's root directory and is the ASP.NET counterpart of the Global.asa of ASP. This file exposes the application and session level events in ASP.NET and provides a gateway to all the application and the session level events in ASP.NET. This file can be used to implement the important application and session level events such as Application_Start, Application_End, Session_Start, Session_End, etc. This article provides an overview of the Global.asax file, the events stored in this file and how we can perform application wide tasks with the help of this file.

What is the Global.asax file?

According to MSDN, "The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET." The Global.asax file is parsed and dynamically compiled by ASP.NET into a .NET Framework class the first time any resource or URL within its application namespace is activated or requested. Whenever the application is requested for the first time, the Global.asax file is parsed and compiled to a class that extends the HttpApplication class. When the Global.asax file changes, the framework reboots the application and the Application_OnStart event is fired once again when the next request comes in. Note that the Global.asax file does not need recompilation if no changes have been made to it. There can be only one Global.asax file per application and it should be located in the application's root directory only.

Events in the Global.asax file

The following are some of the important events in the Global.asax file.

·         Application_Init

·         Application_Start

·         Session_Start

·         Application_BeginRequest

·         Application_EndRequest

·         Application_AuthenticateRequest

·         Application_Error

·         Session_End

·         Application_End

The purpose of these event handlers is discussed in this section below.


The Application_Init event is fired when an application initializes the first time.


The Application_Start event is fired the first time when an application starts.


The Session_Start event is fired the first time when a user’s session is started. This typically contains for session initialization logic code.


The Application_BeginRequest event is fired each time a new request comes in.


The Application_EndRequest event is fired when the application terminates.


The Application_AuthenticateRequest event indicates that a request is ready to be authenticated. If you are using Forms Authentication, this event can be used to check for the user's roles and rights.


The Application_Error event is fired when an unhandled error occurs within the application.


The Session_End Event is fired whenever a single user Session ends or times out.


The Application_End event is last event of its kind that is fired when the application ends or times out. It typically contains application cleanup logic.

Using the Global.asax file

The following code sample shows how we can use the events in the Global.asax file to store values in the Application state and then retrieve them when necessary. The program stores an Application and a Session counter in the Application state to determine the number of times the application has been accessed and the number of users currently accessing the application.

Listing 1

using System;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
public class Global : HttpApplication 
 protected void Application_Start(Object sender, EventArgs e) 
    Application["appCtr"= 0;
    Application["noOfUsers"= 0;
 protected void Application_BeginRequest(Object sender, EventArgs e) 
   Application["appCtr"= (int) Application["appCtr"+ 1;
 protected void Session_Start(Object sender, EventArgs e) 
  Application["noOfUsers"= (int) Application["noOfUsers"+ 1;
 // Code for other handlers

After storing the values in the Application state, they can be retrieved using the statements given in the code sample below.

Listing 2

Response.Write("This application has been accessed "+Application["appCtr"+ " times");
Response.Write("There are "+ Application["noOfUsers"+ " users accessing this application");

The Global.asax file is used in ASP.NET to specify the global objects and the application and the session level events that would be used by the application. It contains all the application and session level events that are used by the application. This article has discussed this file and its events in a lucid language.

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-01-24 10:54:26 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search