Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [80]

By Root 5232 0
that HTTP modules can listen to and handle.

Table 4-7. HttpApplication Events in Order of Appearance

Event

Description

BeginRequest

Occurs as soon as the HTTP pipeline begins to process 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.

ResolveRequestCache, PostResolveRequestCache

Occurs when the ASP.NET runtime resolves the request through the output cache.

MapRequestHandler, PostMapRequestHandler

Occurs when the HTTP handler to serve the request has been found. It is fired only to applications running in classic mode or under IIS 6.

AcquireRequestState, PostAcquireRequestState

Occurs when the handler that will actually serve the request acquires the state information associated with the request.

PreRequestHandlerExecute

Occurs just before the HTTP handler of choice begins to work.

PostRequestHandlerExecute

Occurs when the HTTP handler of choice finishes execution. The response text has been generated at this point.

ReleaseRequestState, PostReleaseRequestState

Occurs when the handler releases the state information associated with the current request.

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.

LogRequest, PostLogRequest

Occurs when the ASP.NET runtime is ready to log the results of the request. Logging is guaranteed to execute even if errors occur. It is fired only to applications running under IIS 7 integrated mode.

EndRequest

Occurs as the last event in the HTTP pipeline chain of execution.

Another pair of events can occur during the request, but in a nondeterministic order. They are PreSendRequestHeaders and PreSendRequestContent.

The PreSendRequestHeaders event informs the HttpApplication object in charge of the request that HTTP headers are about to be sent. The PreSendRequestContent event tells the HttpApplication object in charge of the request that the response body is about to be sent. Both these events normally fire after EndRequest, but not always. For example, if buffering is turned off, the event gets fired as soon as some content is going to be sent to the client. Speaking of nondeterministic application events, it must be said that a third nondeterministic event is, of course, Error.

All these events are exposed by the HttpApplication object that an HTTP module receives as an argument to the Init method. You can write handlers for such events in the global.asax file of the application. You can also catch these events from within a custom HTTP module.

A Custom HTTP Module


Let’s come to grips with HTTP modules by writing a relatively simple custom module named Marker that adds a signature at the beginning and end of each page served by the application. The following code outlines the class we need to write:

using System;

using System.Web;

namespace AspNetGallery.Extensions.Modules

{

public class MarkerModule : IHttpModule

{

public void Init(HttpApplication app)

{

// Register for pipeline events

}

public void Dispose()

{

// Nothing to do here

}

}

}

The Init method is invoked by the HttpApplication class to load the module. In the Init method, you normally don’t need to do more than simply register your own event handlers. The Dispose method is, more often than not, empty. The heart of the HTTP module is really in the event handlers you define.

Wiring Up Events


The sample Marker module registers a couple of pipeline events. They are BeginRequest and EndRequest. BeginRequest is the first event that hits the HTTP application object when the request begins processing. EndRequest is the event that signals the request is going to be terminated, and it’s your last chance to intervene. By handling these two events, you can write custom text to the output stream before and after the regular HTTP handler—the Page-derived

Return Main Page Previous Page Next Page

®Online Book Reader