This file is used by the application to hold application-level events, objects, and variables — all of which are accessible application-wide. Active Server Pages developers had something similar with the Global.asa file.
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="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>
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 2008, 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.
In addition to the global application events that the Global.asax file provides access to, you can also use directives in this file as you can with other ASP.NET pages. The Global.asax file allows for the following directives:
- @Application
- @Assembly
- @Import
These directives perform in the same way when they are used with other ASP.NET page types.
2 comments:
Its very useful.Thanks Varun to share this information
More informative blog. The Keshri Software Solutions is a one-stop site for Asp.Net Live Project Training. We have successfully completed several batches and all are placed and working in a reputed company. Our aim is to provide crystal clear concepts and to boost programming & technology skills of candidates through best Live Project Training.
Please visit : http://training.ksoftware.co.in/ for more details.
Post a Comment