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.
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.
<%@ 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>
<%@ 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>
- 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.
- Application_End()
This event occurs when the application is shutting down, generally because the web server has restarted. You can insert cleanup code here.
- Application_BeginRequest()
This event occurs with each request the application receives, just before the page code is executed.
- Application_EndRequest()
Application_EndRequest() event occurs with each request the application receives, just after the page code is executed.
- Session_Start()
Session_Start() event occurs whenever a new user request is received and a session is started.
- 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 ).
- Application_Error()
Application_Error() event occurs in response to an un-handled error.