, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HttpModules.
The Global.asax file resides in the root directory of an ASP.NET-based application. The Global.asax file is parsed and dynamically compiled by ASP.NET.
The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code written within it.
The Global.asax file does not need recompilation if no changes have been made to it. There can be only one Global.asax file per application and it should be located in the application's root directory only.
Application_EndRequest() – This
event raised at the end of each request right before the objects released.
simple example in Global.asax
First Open visual Studio ---> Create New Website after that right click on Solution explorer and select new item
After select Add New Item window will open in that select Global Application Class and click ok
After add file open Global.asax file and add following code in Global.asax file
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on
application startup
WriteFile("Application
Starting");
}
protected void Application_End(object sender, EventArgs e)
{
// Code that runs on
application shutdown
WriteFile("Application
Ending");
}
protected void Application_Error(object sender, EventArgs e)
{
// Code that runs when an
unhandled error occurs
string strError;
strError =
Server.GetLastError().ToString();
if(Context!=null)
{
Context.ClearError();
Response.Write("Application_Error" + "<br/>");
Response.Write("<b>Error
Msg:</b>" + strError + "<br/>"+"<b>End
Error Msg<b/>");
}
}
protected void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new
session is started
Response.Write("Session_Start" + "<br/>");
}
protected 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.
Response.Write("Session_End"+"<br/>");
}
protected void Application_BeginRequest(object sender,EventArgs e)
{
Response.Write("Application_BeginRequest" + "<br/>");
}
protected void Application_EndRequest(object sender, EventArgs e)
{
Response.Write("Application_EndRequest" + "<br/>");
}
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
Response.Write("Application_AcquireRequestState" + "<br/>");
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
Response.Write("Application_AuthenticateRequest" + "<br/>");
}
protected void Application_AuthorizeRequest(object sender, EventArgs e)
{
Response.Write("Application_AuthorizeRequest" + "<br/>");
}
protected void Application_PostRequestHandlerExecute(object sender, EventArgs e)
{
Response.Write("Application_PostRequestHandlerExecute" + "<br/>");
}
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
Response.Write("Application_PreRequestHandlerExecute" + "<br/>");
}
protected void Application_PreSendRequestContent(object sender, EventArgs e)
{
Response.Write("Application_PreSendRequestContent" + "<br/>");
}
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
Response.Write("Application_PreSendRequestHeaders" + "<br/>");
}
protected void Application_ReleaseRequestState(object sender, EventArgs e)
{
Response.Write("Application_ReleaseRequestState" + "<br/>");
}
protected void Application_ResolveRequestCache(object sender, EventArgs e)
{
Response.Write("Application_ResolveRequestCache" + "<br/>");
}
protected void Application_UpdateRequestCache(object sender, EventArgs e)
{
Response.Write("Application_UpdateRequestCache" + "<br/>");
}
protected void Application_Disposed(object sender, EventArgs e)
{
Response.Write("Application_Disposed"+"<br/>");
}
public void WriteFile(string strText)
{
StreamWriter strWriter
= new StreamWriter(@"C:test.txt", true);
string str
= DateTime.Now + " " + strText;
strWriter.WriteLine(str);
strWriter.Close();
}
</script>
After adding code in Global.asax open your website aspx page and design your aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Global Events Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Global Events</h1>
<asp:Button ID="btnEndSession" runat="server" Text="End Session" onclick="btnEndSession_Click" />
<asp:Button ID="btnError" runat="server" Text="Generate Error" onclick="btnError_Click" />
</div>
</form>
</body>
</html>
After that write the following code in code behind
protected void btnEndSession_Click(object sender, EventArgs e)
{
Session.Abandon();
}
protected void btnError_Click(object sender, EventArgs e)
{
int a = 5;
int b = 0;
int c=a / b;
}