Application_Start vs Application_BeginRequest event in Global.aspx

Published on Thursday, October 21, 2010

If you use Global.aspx in your ASP.NET project, you will see 2 different but look like the similar events: Application_Start and Application_BeginRequest

The differents:

Application_Start

Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values.

You should set only static data during application start. Do not set any instance data because it will be available only to the first instance of the HttpApplication class that is created.

Application_BeginRequest

It is the one of Application_event which is raised at the appropriate time in the application life cycle .

Application_BeginRequest is raised when any Web Forms page or XML Web service in your application is requested. This event allows you to initialize resources that will be used for each request to the application. A corresponding event, Application_EndRequest, provides you with an opportunity to close or otherwise dispose of resources used for the request.

If two modules handle the Application_BeginRequest event and the first one throws an exception, the Application_BeginRequest event will not be called for the second module. However, the Application_EndRequest method is always called to allow the application to clean up resources.

For detail information please check MSDN: ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0

A related kind of event is Session events are similar to application events (there is a Session_Start and a Session_End event), but are raised with each unique session within the application. A session begins when a user requests a page for the first time from your application and ends either when your application explicitly closes the session or when the session times out.