ASP.NET application use a special file called Global.asax to maintain information about events and objects that are global to the application. This file is stored in the root directory of the application. An application can have only one Global.asax file. The server processes the Global.asax file when:
- The user starts or stops application.The Application_Start event is fired when application is started. The Application_End event is triggered when the user ends the application.
- A session starts or stops. The Session_Start event is triggered when an individual session is started for a user who accesses the application. The Session_End event is fired when the user ends the session.
Global.asax | |
Application Events |
|
Session Events |
|
The following table describes some of the events stored in Global.asax file:
Sample Global.asax File:
Output:
If the page is refereshed, the output would be:
(Note: We cannot include any HTML tags or Response.Write() method in Application_Start() because session has not yet started and Response object has not been created.)
Event | Description |
---|---|
Application_Start | Fires when the first ASP.NET page in the current application is called. |
Application_BeginRequest | Fires every time when a page is loaded or refreshed. |
Application_EndRequest | Fires when a page request ends |
Application_End | Fires when the last session of the application ends. |
Session_Start | Fires every time a new session starts. |
Session_End | Fires when session ends. |
Sample Global.asax File:
protected void Application_Start(object sender, EventArgs e) { } protected void Session_Start(object sender, EventArgs e) { Response.Write("Session Started"); } protected void Application_BeginRequest(object sender, EventArgs e) { Response.Write("Application Request Begins"); } protected void Application_EndRequest(object sender, EventArgs e) { Response.Write("Application Request ends"); } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { Response.Write("Session Ended"); } protected void Application_End(object sender, EventArgs e) { } |
Output:
Application Request Begins Session Started Application Request ends |
If the page is refereshed, the output would be:
Application Request Begins Application Request ends |
(Note: We cannot include any HTML tags or Response.Write() method in Application_Start() because session has not yet started and Response object has not been created.)
No comments:
Post a Comment