Friday, May 16, 2008

Global.asax in ASP .NET

The Global.asax, which also known as ASP .NET application file, is located in the root directory of an ASP .NET application. This file contains codes that are executed in response to application-level and session-level events rose by ASP .NET or by HTTP modules.

There are a few things that needs to be take note about Global.asax, as below:
 The Global.asax is an optional file. You can remove the file if it is unnecessary.
 Any direct URL request to retrieve this file will be rejected automatically. External users are restricted from download or view the code written within it.
 If there are any changes on an active Global.asax file, the ASP .NET page framework will firstly complete all current requests for the application, sends the Application_End event to any listeners, and then restarts the application domain. In effect, this reboots the application, closing all browser sessions and flushing all state information. When the next incoming request from a browser arrives, the ASP.NET page framework re-parses and recompiles the Global.asax file and raises the Application_Start event.

In Visual Studio 2005, the Global.asax is not created by default. To create a Global.asax in your web project, right click your web site or web project in your solution explorer, choose Add New Item from the context menu. From the Visual Studio Installed Template, select Global Application Class and you can see something similar to the figure below:



Application and Session Events in Global.asax

Application Handlers:
Application_Start – invoked when the application first starts
Application_Init – invoked after Application_Start and is used for initializing code
Application_Disposed – invoked before destroying an instance of an application
Application_Error – invoked when an exception occurs
Application_End – invoked when the application ends and used to clean up variables and memory

Request Handlers:
Application_BeginRequest – invoked when an user makes a request for the application
Application_EndRequest – invoked at the end of each request
Application_PreRequestHandlerExecute – invoked before ASP .NET executes an event handler
Application_PostRequestHandlerExecute – invoked after ASP .NET handler has finished its execution
Application_PreSendRequestHeaders – invoked before ASP .NET sends HTTP header to the client
Application_PreSendRequestContent – invoked before ASP .NET sends contents to the client
Application_AuthenticateRequest – invoked before a user credentials are authenticated
Application_AuthorizeRequest – invoked on successful authentication of a user credentials. You can create your own user authorization to access the resources of an application
Application_ResolveRequestCache – invoked on successful completion of the authorization request
Application_AcquireRequestState – invoked when ASP .NET acquires the current state associated with the current request
Application_ReleaseRequestState – invoked before current state data in the session collection is serialized
Application_UpdateRequestCache – invoked before information is added to output cache of the page

Session Methods:
Session_Start – invoked when session starts on each user who requesting a page
Session_End – invoked when the session of a user times out or ends

0 comments:

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
Sign up for PayPal and start accepting credit card payments instantly. http://www.emailcashpro.com
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template