3

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?

2

2 Answers 2

5

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.

1

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.

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.