| An ASP.NET application is made up of a collection of files including Web forms and assemblies that are stored in a virtual directory marked as an IIS application. When a resource in an ASP.NET application is requested , ASP.NET instantiates an HttpApplication object that then takes over the processing of the incoming request. The HttpApplication class defines the methods , properties, and events common to all application objects in an ASP.NET application. If you want to customize the behavior of an HttpApplication object, you can derive a class from the HttpApplication class and override the event handlers of the base class for various application-level events. An easy way to do this is by using the global.asax file. The global.asax File The global.asax file defines a class named Global that derives from the HttpApplication class. When ASP.NET notices that the global.asax file is present in the root directory of an application, rather than using the implicitly created HttpApplication object, ASP.NET creates instances of the class defined in the global.asax file to handle requests for an application. | For security reasons, ASP.NET restricts users of an application from downloading any file with the extension .asax . | Global Event Handlers The global.asax file is an appropriate place to handle events that are not specific to a Web form, but rather apply to an application as a whole. I'll call these events global events and classify them in two categories application- and session-level events and per-request events . Application- and Session-level Events Application- and session-level events are fired to signal the start and end of the application or user session. These events can be handled using the predefined event handlers in the global.asax file shown in Table 4.8. Table 4.8. Application- and Session-level Event Handlers in the global.asax File | Event Handler | Purpose | | Application_Start() | Called when an application receives its first request. It's generally used to initialize data that is shared among all users of an application. | | Application_End() | Called when an application shuts down. Here you can write code to persist the information stored in memory that you want to have reloaded when the application restarts. | | Session_Start() | Called when an ASP.NET application creates a new session for a user of the application. | | Session_End() | Called when the user's session expires . By default, this happens 20 minutes after the last request of a page from a user. | Per-request Events The event handlers shown in Table 4.9 are invoked for each individual page request processed by the HttpApplication object. Table 4.9. Per-request Event Handlers | Event Handler | Purpose | | Application _ BeginRequest() | Called at the beginning of each request | | Application _ AuthenticateRequest() | Called when a security module has established the identity of the user | | Application_AuthorizeRequest() | Called when a security module has verified user authorization | | Application_ResolveRequestCache() | Called to resolve the current request by providing content from a cache | | Application_AcquireRequestState() | Called to associate the current request with the session state | | Application_PreRequestHandlerExecute() | Called when ASP.NET begins executing a page | | Application_PostRequestHandlerExecute() | Called when ASP.NET finishes executing a page | | Application_ReleaseRequestState() | Called to save the current state data | | Application_UpdateRequestCache() | Called to update a cache with the responses | | Application_EndRequest() | Called at the end of each request | As you can see from Table 4.9, you have complete control over how a request is processed. You can write code in any of these event handlers to modify the default behavior of ASP.NET. The following example uses the Application_BeginRequest() and Application_EndRequest() methods to determine the time it takes for each request to process and append this information with every response: -
Open the global.asax file for the project Example4_2 and switch to Code view. -
Add the following code to the Application_BeginRequest() event handler: protected void Application_BeginRequest(Object sender, EventArgs e) { // Store the begin time of the request in the HttpContext object this.Context.Items.Add("BeginTime", DateTime.Now); } -
Add the following code to the Application_EndRequest() event handler: protected void Application_EndRequest(Object sender, EventArgs e) { // Get the begin time from the HttpContext object DateTime dtBeginTime = (DateTime) this.Context.Items["BeginTime"]; // Calculate the time span between the start and end of request TimeSpan tsProcessingTime = DateTime.Now-dtBeginTime; // Display the processing time taken by the response this.Context.Response.Output.Write("<hr>"); this.Context.Response.Output.Write( "{0} took {1} milliseconds to execute.", this.Request.Url, tsProcessingTime.TotalMilliseconds); } -
Run the project. You should see that the page shows a message at the bottom indicating the processing time of the request. We used the Context object in the preceding program to store the begin time. The Context object provides access to all the information about the current HTTP request. It also exposes a key-value collection via the Items property in which you can add values that will be available for the life of the current request. |