X



Home>Tutorial>

ASP.Net Global.asax File and Events Handling

Nuelson Penuel Saturday, August 29, 2020 ASP.Net

Global.asax in ASP.net

The Global.asax is also known as the ASP.NET application file and is used to serve application-level and session-level events. The Global.asax file is optional. If you do not define the file, the ASP.NET page framework assumes that you have not defined any application or session event handlers.

Characteristics of Global.asax

1. It allows us to write code that response to global application events raised by ASP.NET or by HttpModules. 2. These events fire at various points during the lifetime of a web application, including when the application domain is first created. 3. The Global.asax file resides in the root directory of an ASP.NET-based application 4. At run time, global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class.

ASP.Net VB Global.asax File

<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
    End Sub
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub
        
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
        
        
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' 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.
    End Sub
       
</script>

ASP.Net C# Global.asax File

<%@ 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>

ASP.Net Global.asax Event Handling Method

  1. Application_Start() This event occurs when the application starts, which is the first time it receives a request from any user. It doesn’t occur on subsequent requests. This event is commonly used to create or cache some initial information that will be reused later.
  2. Application_End() This event occurs when the application is shutting down, generally because the web server has restarted. You can insert cleanup code here.
  3. Application_BeginRequest() This event occurs with each request the application receives, just before the page code is executed.
  4. Application_EndRequest() Application_EndRequest() event occurs with each request the application receives, just after the page code is executed.
  5. Session_Start() Session_Start() event occurs whenever a new user request is received and a session is started.
  6. Session_End() Session_End() event occurs when a session times out or is programmatically ended. This event is only raised if you are using in-process session state storage (the InProc mode, not the StateServer or SQLServer modes ).
  7. Application_Error() Application_Error() event occurs in response to an un-handled error.

| | Views(841)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services