aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

What is the use of Global.asax file events in

May 18, 2011

Here I will explain what is the purpose of Global.asax file and how we can use Global.asax file in and I will explain application level events in global.asax file.


The Global.asax file, 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.
The Global.asax contains two types of events those are

Events which are fired for every request

Events which are not fired for every request

Now I will explain about

Events which are fired for every request

Application_BeginRequest() – This event raised at the start of every request for the web application.

Application_AuthenticateRequest – This event rose just before the user credentials are authenticated. We can specify our own authentication logic here to provide custom authentication.

Application_AuthorizeRequest() – This event raised after successful completion of authentication with user’s credentials. This event is used to determine user permissions. You can use this method to give authorization rights to user. 

Application_ResolveRequestCache() – This event raised after completion of an authorization request and this event used in conjunction with output caching. With output caching, the rendered HTML of a page is reused without executing its code.

Application_AcquireRequestState() – This event raised just before session-specific data is retrieved for the client and is used to populate Session Collection for current request.

Application_PreRequestHandlerExecute()This event called before the appropriate HTTP handler executes the request.

Application_PostRequestHandlerExecute()This event called just after the request is handled by its appropriate HTTP handler.

Application_ReleaseRequestState() – This event raised when session specific information is about to serialized from the session collection.

Application_UpdateRequestCache() – This event raised just before information is added to output cache of the page.

Application_EndRequest() – This event raised at the end of each request right before the objects released. 

Now we will see

Events which are not fired for every request

Application_Start() – This event raised when the application starts up and application domain is created.

Session_Start() – This event raised for each time a new session begins, This is a good place to put code that is session-specific.

Application_Error() – This event raised whenever an unhandled exception occurs in the application. This provides an opportunity to implement generic application-wide error handling.

Session_End() – This event called when session of user ends.

Application_End() – This event raised just before when web application ends.

Application_Disposed() – This event fired after the web application is destroyed and this event is used to reclaim the memory it occupies.

Now we will see all these events with 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();
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.
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)
public void WriteFile(string strText)
StreamWriter strWriter = new StreamWriter(@"C:test.txt", true);
string str = DateTime.Now + " " + strText;
After adding code in Global.asax open your website aspx page and design your aspx page like this

<html xmlns="">
<head runat="server">
<title>Global Events Demo</title>
<form id="form1" runat="server">
<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" />
After that write the following code in code behind

protected void btnEndSession_Click(object sender, EventArgs e)
protected void btnError_Click(object sender, EventArgs e)
int a = 5;
int b = 0;
int c=a / b;
Now run your application and see the output you will find events which are fired for every request

If you click End Session button that will Ends session and see how the output is

If you click on General Error button see how the output is

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

hi i m arun narwal and
i want to create a blog so please help me to create a my own blog and help me how to create a blog no body can help me so please help me

jidheesh Rajan said...

good post..

Anonymous said...

Thank U it is informative

Anonymous said...

Very informative post. Information conveyed in a very simple and crisp manner.

Kapil Waghe said...

Awesome post... thanks

Vinayak Deshpande said...

thanks for the post sir...

Anonymous said...

Suresh you have always been so much helpful with all your posts . Thank u

Anonymous said...

Thanks Suresh for Sharing your Knowledge

Mllikarjuna said...

Very Nice Suresh,.....

Good One Keep Rocking.....

sai nadh said...

very nice post

muns said...


Anonymous said...

good example.but i have to know more about global.ascx.where it is used in the real time project and how it is used with example?if any body knows make me a sence ....

Ragul said...

//Response.Write("Session_End" + "

is not working in Global.asax file in
protected void Session_End method

powerful peraiah said...

I want to update my database when a browser close manually or in case the power off.

Anonymous said...

Hello Sir,

You don't even have an idea about the impression and motivation on me. In future when i have my own Blog, you are also one of the key motivators, thank you for all these stupendous articles you have shared to developers, especially the young people like me. All the best to you. God bless you.

gattupradee said...

Really good work, easy to understand..

siva's thoughts said...

good example.but i have to know more about global.ascx.where it is used in the real time project and how it is used with example?if any body knows make me a sence ....

Anonymous said...

(Y) good 1

Anonymous said...

very nice, i am a big follower of your blog

Anonymous said...


Anonymous said...

could you please explain, global exception handling using global.asax file

Anonymous said...

anyone have e-auction project in please send me to this id

Hari Krishna said...

can you please provide custom validation examples more?

Anonymous said...

At which event Global.asax file called

Anonymous said...

Hi kiran i got error like this:


iam geting error in Global.asax page in 4.0.Please help me how to clear that error

The error deatails:

"Object reference not set to an instance of an object." this message can be dispaly with in following code:

void Session_End(object sender, EventArgs e)

HttpContext.Current.Response.Write("Session_End" + "
// 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.


How to clear this error anybody help me soon very urgent now

Advance thank you to very viewers and answers also.

AjitKumarVermaa said...

Good Work

Anonymous said...

good example bt i want to know more about global.asax

BVSN.Dinesh said...

Hi Suresh Anna,

I am using MasterPage in my project now my requirement is to show popup when session timeout. I used javasript function and calling that function in master(.cs)page in that age working fine but in children page not, plz clarify my doubt in any other way as early as possible

puni said...

Hi All,

I need to abandon session on closing browser instead of pressing log out button. Your reply is much needed for me..

syed khaleel said...

nice post suresh

manish jadhav said...

Thank you..
Nice Explanation.. :)

Anonymous said...


Anonymous said...

Thank You for post

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.