James South
James South

Reputation: 10635

PreSendRequestHeaders alternative in a HttpModule

According to this information given by the Asp.Net team What not to do in Asp.net you should not use PreSendRequestHeaders in a managed HttpModule.

PreSendRequestHeaders and PreSendRequestContext

Recommendation: Do not use these events with managed modules.

The PreSendRequestHeaders and PreSendRequestContext events can be used with native IIS modules, but not with managed modules that implement IHttpModule. Setting these properties can cause issues with asynchronous requests.

This is precisely what I do in my Image Processing Library to ensure that the correct mime type is sent along with the response.

What would be the recommended alternative approach?

Upvotes: 3

Views: 2872

Answers (2)

chkimes
chkimes

Reputation: 1137

In a handler for BeginRequest, use HttpResponse.AddOnSendingHeaders to subscribe a handler for that event.

This is essentially the same thing as PreSendRequestHeaders, however it is handled fully within the ASP.NET pipeline so it doesn't have the same problems with the native/managed interface that PreSendRequestHeaders does.

Upvotes: 5

mikesjawnbit
mikesjawnbit

Reputation: 106

If you look at the HttpApplication pipeline on MSDN's ASP.NET App Life Cycle page you'll find PreSendRequestHeaders and PreSendRequestContent events at the very bottom. Now that they're suggesting we not use these in managed IHttpModules, the next closest event in the pipeline would be EndRequest, which is deterministic unlike PreSendRequest* events.

I haven't been able to find any information on what the preferred practice is now for specifying response headers, but this will work for you.

Upvotes: 1

Related Questions