GLOBAL. ASAX FILES

GLOBAL. ASAX FILES

Posted on

GLOBAL. ASAX FILES

Global.asax is a class file that is used to specify the session and application event handlers for a web application. The Global.asax file resides in the root directory of an asp.net web application and is also called the ASP.NET application file. This file contains the code that is executed when certain events, such as start of an application or error in an application, are raised by an ASP.NET web application.

Events and states, such as session state and application state, which are specified in the Global.asax file, are applied to all the resources of the web application. For example, if an application state variable is defined in the Global.asax file, then all the .aspx files with the root directory can access the variable.

The code in the Global.asax file is written in the same way as in web forms; the only difference is that the code written in the Global.asax file does not contain HTML or ASP.NET tags. Instead, the code in this file contains methods with predefined names.

¾    The Global.asax file added to the ASP.NET web application contains empty event handlers for the commonly used application events, such as the start and end of an application. The code for the event handlers is inserted in the appropriate method of the Global.asax file. The name of the method for which an event hadler is created should be similar to the name of the event occurring in the web application. For example, the Application_start() method is called when the Application_Start event occurs in the ASP.NET web application.

The Global.asax file can be used either for initializing the application and session state variables or clearing out the memory by destroying all such variables. The Global.asax file consists of following events that can be handled using it:

  • Application_Init: Fires when the application initializes for the first time.
  • Application_Start: Fires the first time an application starts.
  • Session_Start: Fires the first time when a user’s session is started.
  • Application_BeginRequest: Fires each time a new request comes in.
  • Application_EndRequest: Fires when the request ends.
  • Application_AuthenticateRequest: Indicates that a request is ready to be authenticated.
  • Application_Error: Fires when an unhandled error occurs within the application.
  • Session_End: Fires whenever a single user Session ends or times out.
  • Application_End: Fires when the application ends or times out (Typically used for application cleanup logic).