Understanding the Global.asax file

This article provides an overview of the global.asax file and how we can perform application wide tasks like global exception and error handling with the help of the same.
by Joydip Kanjilal
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 2490/ 880

Introduction

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.

Application_Init

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

Application_Start

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

Session_Start

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

Application_BeginRequest

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

Application_EndRequest

The Application_EndRequest event is fired when the application terminates.

Application_AuthenticateRequest

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.

Application_Error

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

Session_End

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

Application_End

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.Lock();

               Application["appCtr"= (int) Application["appCtr"+ 1;

               Application.UnLock(); 

             }

             

             protected void Session_Start(Object sender, EventArgs e) 

             {

              Application.Lock();

              Application["noOfUsers"= (int) Application["noOfUsers"+ 1;

              Application.UnLock(); 

             }

             // 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");
References

Working with the ASP.NET Global.asax file

ASP.NET Application Life Cycle Overview (MSDN)

A Detailed View of the Global.asax File

Conclusion

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.

你可能感兴趣的:(global)