HttpApplication Events Now that you've seen how to implement an HttpModule, it's time to look at the events that you can handle and when they are called. The HttpApplication object has several events that can be handled by an HttpModule. Table 25.1 lists these events. Because any number of HttpModules can handle these events, take particular care if you need to have your HttpModule be the only one that handles a given event. The only way to guarantee that no other HttpModule handles an event is to know which other HttpModules are registered for your Web application and to know what events are handled by the other HttpModules. For example, you have two HttpModules that handle the AuthenticateRequest event. These two HttpModules provide different types of authentication. Because the Web client might be able to provide data for only one type of authentication, one of the HttpModules will always be unable to authenticate the request. In this case, the request will always be rejected by one of the HttpModules. Table 25.1. HttpApplication Events Event | Description | AcquireRequestState | Called before the intrinsic Requestobject is set up. | AuthenticateRequest | Called to allow authentication of a request. | AuthorizeRequest | Called to allow authorization of a request. | BeginRequest | Called at the beginning of the request before any other processing has been done. | Disposed | Called during the Applicationobject's disposal process, when the application is shutting down. | EndRequest | Called after all processing of the request has been done. | Error | Called if there was an unhandled error during the processing of the request. | PostRequestHandlerExecute | Called after the request handler has finished executing. The request handler is usually a class derived from the System.Web.UI.Pageclass. | PreRequestHandlerExecute | Called after all pre-request processing (including setting up the intrinsic Requestobject) has been completed, but before the request handler is called. | PreSendRequestContent | Called before the body of the response is sent. | PreSendRequestHeaders | Called before the response headers are sent. | ReleaseRequestState | Called after the intrinsic Requestobject's data is released. | ResolveRequestCache | Called after the request cache is resolved. | UpdateRequestCache | Called after the request cache is updated. | A request can be terminated by calling the HttpApplication member function CompleteRequest from the event handler. If CompleteRequest is not called, the application will continue to process the request as normal. If you write an event handler to handle the AuthenticateRequest event, you would need to call CompleteRequest if the authentication fails. It is important to consider what has happened and what is going to happen before and after each event when deciding which event you should handle. If you handle the wrong event, you might not generate the expected output. The following sequential list is the timeline of how a request is processed from the viewpoint of an HttpModule: -
The server receives the request and passes it to your Web application. -
The BeginRequest event is fired . -
The AuthenticatRequest event is fired. A handler of this event authenticates the request. -
The AuthorizeRequest event is fired. A handler of this event authorizes the request. -
The ResolveRequestCache event is fired. -
The request cache is resolved. -
The AcquireRequestState event is fired. -
The PreRequestHandlerExecute event is fired. -
The request handler is executed. Normally this means that a System.Web.UI.Page derived class is created that executes your .aspx file. -
The PostRequestHandlerExecute event is fired. -
The ReleaseRequestCache event is fired. -
The request cache is released. -
The UpdateRequestCache event is fired. -
The request cache is updated. -
The EndRequest event is fired. Processing of the request is now done. -
The PreSendRequestHeaders event is fired. This event can actually be fired any time after the request handler starts executing if the Response object is not buffered. -
The response headers are sent. -
The PreSendRequestContent event is fired. This event can be fired any time after the PreSendRequestHeaders event is fired. It also can be fired multiple times if the content is sent in multiple sections. -
The response content is sent. If the Response object is not buffered, this may contain only part of the total response content. The Error event is fired if there is an unhandled error during the processing of the request. The Disposed event is fired when the Dispose method of the HttpApplication class is called during the final shutdown of an application. |