Monday 23 November 2015

Global.asax file in Asp.Net.

Introduction:
 
I will explain about what is global.asax file and use of global.asax file.
 
Explanation:

If your application does not have global.asax file you can add  a new item to your ASP.NET application, you get the Add New Item dialog. From here, you can see that you can add a Global Application Class to your applications. This adds a Global.asax file. This file is used by the application to hold application-level events, objects, and variables - all of which are accessible application-wide.

Your ASP.NET applications can have only a single Global.asax file. This file supports a number of items. When it is created, you are given the following template:

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
      
</script>

Just as you can work with page-level events in your .aspx pages, you can work with overall application events from the Global.asax file. In addition to the events listed in this code example, the following list details some of the events you can structure inside this file:

•    Application_Start: Called when the application receives its very first request. It is an ideal spot in your application to assign any application-level variables or state that must be maintained across all users.

•    Session_Start: Similar to the Application_Start event except that this event is fired when an individual user accesses the application for the first time. For instance, the Application_ Start event fires once when the first request comes in, which gets the application going, but the Session_Start is invoked for each end user who requests something from the application for the first time.

•    Application_BeginRequest: Although it not listed in the preceding template provided by Visual Studio, the Application_BeginRequest event is triggered before each and every request that comes its way. This means that when a request comes into the server, before this request is processed, the Application_BeginRequest is triggered and dealt with before any processing of the request occurs.

•    Application_AuthenticateRequest: Triggered for each request and enables you to set up custom authentications for a request.

•    Application_Error: Triggered when an error is thrown anywhere in the application by any user of the application. This is an ideal spot to provide application-wide error handling or an event recording the errors to the server’s event logs.

•    Session_End: When running in InProc mode, this event is triggered when an end user leaves the application.

•    Application_End: Triggered when the application comes to an end. This is an event that most ASP.NET developers won’t use that often because ASP.NET does such a good job of closing and cleaning up any objects that are left around.

No comments:

Post a Comment