Programming Microsoft ASP.NET 4 - Dino Esposito [331]
In addition to the operational methods in Table 16-3, a few other HttpApplication methods are available to register asynchronous handlers for application-level events. These methods are of little interest to user applications and are used only by HTTP modules to hook up the events generated during the request’s chain of execution.
Events of the HttpApplication Class
Table 16-4 describes the event model of the HttpApplication class—that is, the set of events that HTTP modules, as well as user applications, can listen to and handle.
Table 16-4. HttpApplication Events
Event
Description
AcquireRequestState, PostAcquireRequestState
Occurs when the handler that will actually serve the request acquires the state information associated with the request.
AuthenticateRequest, PostAuthenticateRequest
Occurs when a security module has established the identity of the user.
AuthorizeRequest, PostAuthorizeRequest
Occurs when a security module has verified user authorization.
BeginRequest
Occurs as soon as the HTTP pipeline begins to process the request.
Disposed
Occurs when the HttpApplication object is disposed of as a result of a call to Dispose.
EndRequest
Occurs as the last event in the HTTP pipeline chain of execution.
Error
Occurs when an unhandled exception is thrown.
LogRequest, PostLogRequest
Occurs when the system logs the results of the request.
PostMapRequestHandler
Occurs when the HTTP handler to serve the request has been found.
PostRequestHandlerExecute
Occurs when the HTTP handler of choice finishes execution. The response text has been generated at this point.
PreRequestHandlerExecute
Occurs just before the HTTP handler of choice begins to work.
PreSendRequestContent
Occurs just before the ASP.NET runtime sends the response text to the client.
PreSendRequestHeaders
Occurs just before the ASP.NET runtime sends HTTP headers to the client.
ReleaseRequestState, PostReleaseRequestState
Occurs when the handler releases the state information associated with the current request.
ResolveRequestCache, PostResolveRequestCache
Occurs when the ASP.NET runtime resolves the request through the output cache.
UpdateRequestCache, PostUpdateRequestCache
Occurs when the ASP.NET runtime stores the response of the current request in the output cache to be used to serve subsequent requests.
To handle any of these events asynchronously, an application will use the corresponding method whose name follows a common pattern: AddOnXXXAsync, where XXX stands for the event name. To hook up some of these events in a synchronous manner, an application will define in the global.asax event handler procedures with the following signature:
public void Application_XXX(Object sender, EventArgs e)
{
// Do something here
}
Of course, the XXX placeholder must be replaced with the name of the event from Table 16-4. All the events in the preceding table provide no event-specific data. You can also use the following simpler syntax without losing additional information and programming power:
public void Application_XXX()
{
// Do something here
}
In addition to the events listed in Table 16-4, in global.asax an application can also handle Application_Start and Application_End. When ASP.NET is about to fire BeginRequest for the very first time in the application lifetime, it makes Application_Start precede it. EndRequest will happen at the end of every request to an application. Application_End occurs outside the context of a request, when the application is ending.
As you saw in Chapter 2, application events are fired in the following sequence:
BeginRequest The ASP.NET HTTP pipeline begins to work on the request. This event reaches the application after Application_Start.
AuthenticateRequest The request is being authenticated. All the internal ASP.NET authentication modules subscribe to this event and attempt to produce an identity. If no authentication module produced an authenticated user, an internal default authentication module