Understanding Global.asax

In this article i am explain you about Global.asax file. Global.asax is a Global Application file which handles all application and session level events. This is opetional file exist in Application context

Understanding Global.asax

History behind this article is very funny, my friend need to calculate the hit count to his website, he was under stressed from last 3 days , finally i have told him to use Global.asax to accomplish task. So i thought to write an article over Global.asax, and now i get chance to serve you.

This is optional file in ASP.NET, this file also known as ASP.NET application file. This file takes care of all application and session level events raised by ASP.NET or by HTTP modules. This file can be found in the root directory of an ASP.NET-based application. This is very secure, external users cannot download or view the code in it. This file exposes the application and session level events in ASP.NET. This is the gateway for all session and application event Global.asax file is the central point for ASP.NET applications. It provides numerous events to handle various application-wide tasks such as user authentication, application start up, and dealing with user sessions.

if Global.asax is not present in your solution explorer then you can add it by using Right click on projectname in solution explorer and select Add Item and select "Global application class"



Let's see Global.asax events one by one

Global.asax events

Global.asax file derived from the HttpApplication class maintains a pool of HttpApplication objects and assign them to application on need basis.
Here we go for Global.asax events

This is the very first event called by of application when an application get initializes

After initializes, Application_Start event get fired when instance of the HttpApplication class is created.

This event get's called when the last instance of an HttpApplication class is destroyed. It is called only once during an application's lifetime.

This evevnt used by application before it is destroyed. This is the ideal location for cleaning up previously used resources and objects if application throws unhandled exception.

If we have not used TRY...CATCH in code then application handle this exception in this event, this event get called when unhandled exception occurred.

This is event is user specific, which will fired when any new user opens webapplication, at same time new session ID is allocate to that user.

This event called when session timeout is reached or session endup by calling Session.Abandon method.

These are the main events that we use each time, but still there are some other events too. enlisted below,

1. Application_BeginRequest
2. Application_EndRequest
3. Application_PreRequestHandlerExecute
4. Application_PostRequestHandlerExecute
5. Applcation_PreSendRequestHeaders
6. Application_PreSendContent
7. Application_AcquireRequestState
8. Application_ReleaseRequestState
9. Application_ResolveRequestCache
10. Application_UpdateRequestCache
11. Application_AuthenticateRequest
12. Application_AuthorizeRequest

Let's use Global.asax file

Example 1 :
Now we will calculate website hit count with the help of global.asax, it's very simple.
To solve problem we need Application_Start and Session_Start event, when application accessed fir first time then initialize HityCounter to 0
on each new session initialization increment HitCounter. after that on Main menu page you shoe the website hit counter


Example 2 :
There are centain conditions where we can't use Try..Catch so how to show error and un handled exceptions.
don't worry...Global.asax will help you.
Add a Error page in your code for showing error occur in website. say Error.Aspx and direct to that page if Error occur
We can use Application_Error event
we can use Server.GetLastError() method to get exception


When you change any active Global.asax file then the ASP.NET page framework detects that file changed by someone and send request to

Application_OnEnd event and restarts the application domain.
In effect, this will reboots the application by closing all browser sessions and flushing all state information. on the next request it compiles the Global.asax file and raises the Application_OnStart event to start the application.

In short
It's a Global application file that handles all application and session level events. With the help of this file we can keep all our sharable data in Application object and can use by other users.
In next article we can elaborate Application object.

Suggestion and Queries always welcome



Guest Author: Twam20 Mar 2012

I have just tried to implement the July 11th Update on an Umbraco 4.5 site and I was ubnlae to get it working.I then implemented the July 12th Update and it worked immediatley.I can therefore confirm that creating a global.asax that inherits umbraco.Global works great with 4.5

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: