ASP.NET Application Phases
 
An Example for Using Global.asax
 
How to add a Global.asax file to the Web Site or Web Application?
 
1. Click the “Website? Menu and then click on “Add New Item? to open the “Add New Item? dialog box.
 
 
2. In “Add New Item? dialog box you will find “Global Application Class? item select it and press “Add? button.

3. To open the “Global Application Class? events, select it in the Solution Explorer? and double click it then you will to get all the events of the “Global Application Class? as given the figure below.
 
 
The “Global Application Class? events code is are given below:
 
Here first line uses the “Application? Directive.
 
<%@ 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.
}
</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.
}
</script>