Showing posts with label Global.asax. Show all posts
Showing posts with label Global.asax. Show all posts

Monday 7 December 2015

Global.asax file in ASP.Net

In this post, I will explain to you about Global.asax file and Events in the Global.asax file.
A 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.asax 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 the last event of its kind that is fired when the application ends or times out. It typically contains application cleanup logic.

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.