This tutorial was brought to you by NetroStar, a Miami-based global web design
Global.asax file is used to handle various application events raised by ISS server. You cannot receive such events in standard page code and you have to use Global.asax instead simply because pages doesn't exist in that moment. Consider the start of the application, when this event occurs all web pages are not loaded yet (the same goes for the end of the application). Global.asax file has to be in server's virtual root directory to work well.
To handle properly application events it's good to have an idea how does application live cycle look like. During this cycle application is initialized and various events such as Application_Start or Application_Init are raised. You can handle these events in Global.asax file.
Application life cycle
Application life cycle starts with first request received by web server. Then server passes this request to the proper service that handles this request. Server decides to which service it should send the request simply by file extension.
Then after first request was received an application domain is created by so called ApplicationManager (class that is used to manage application domains). Application domain is a mechanism used to isolate various applications from others. Each one has its own scope of variables and resources. You can compare application domain to a process in operating system. Application domain creates class HostingEnvironment that allows you to manage application by providing application management functions.
In the next step all application core objects such as HttpRequest or HttpResponse are created. Also HttpContext object is created that contains all information concerning HTTP request.
Then HttpApplication object is created. If an application has Global.asax file than a class within this file inherits from HttpApplication class, which defines methods and properties that are common for all application objects.
Finally the request is processed by HttpApplication object. Then various events are raised which can be handled by handlers defined in Global.asax file. The complete list of these events will be covered later in this tutorial.
Take a look at the visual summary of application life cycle:
Events raised during application life cycle
There are various events raised through different stages of application life cycle. You can subscribe to them in Global.asax file to perform necessary operations. If you don't have Global.asax file in your application just create it in root directory. Event handler in Global.asax file are automatically bound to application events using Application_event pattern so it saves you amount of code you have to write.
Take a look at the following list of events raised during application life cycle:
- Application_Start - raised only once when the very first resource from application is requested
- Init - called each time when instance of the HttpApplication is created
- Events that are raised by instance of HttpApplication class will be covered in the next section
- Dispose - called each time when instance of the HttpApplication is destroyed
- Application_End - raised only once when the application terminates
Events raised by instance of HttpApplication class
During processing a request in HttpApplication object the following events are raised:
- Application_BeginRequest - raised when ASP.NET receives a request
- Application_AuthenticateRequest - raised when user is being authenticated (that means that identity of a user is being checked)
- Application_PostAuthenticateRequest - raised when user is already authenticated
- Application_AuthorizeRequest - raised when user authorization is being checked (that means that application checks if user has sufficient rights to perform a concrete operation)
- Application_PostAuthorizeRequest - raised when request from a user is already authorized
- Application_ResolveRequestCache - raised when caching modules serve request from cache
- ApplicationPostResolveRequestCache - raised when execution of current event handler is bypassed
- Application_PostMapRequestHandler - raised when current request is already mapped to the appropriate event handler
- Application_AcquireRequestState - raised when the current state is being acquired
- Application_PostAcquireRequestState - raised when the current state is already acquired
- Application_PreRequestHandlerExecute - raised when event handler starts being executed
- Application_PostRequestHandlerExecute - raised when execution of event handler is finished
- Application_ReleaseRequestState - raised when execution of all event handlers has been finished
- Application_PostReleaseRequestState - raised when all request state data has been saved
- Application_UpdateRequestCache - raised when the execution of event handler is finished to allow caching modules to store responses
- Application_PostUpdateRequestCache - raised when storing of responses is finished
- Applicattion_EndRequest - raised when the last event responds to a request
- Application_PreSendRequestHeaders - raised before Http header is send to the client
- Application_PreSendRequestContent - raised before content is send to the client
Global.aspx file is a core of ASP.NET applications. It allows you to respond to various events such as beginning or ending of processing request. You can deal there also with security issues using events such as AuthenticateRequest or AuthorizeRequest. So it's good to know how in fact does the Global.aspx file work. It can be useful in web design.