0

In the BeginRequest event of my app, I am checking if the request is http and calling Response.Redirect to redirect to https. Here the code snippet:

if (shouldRedirectToHttps)
{
    var newUrl = "https:" + requestUrl.ToSchemelessUri();
    httpContext.Response.Redirect(newUrl);
    return;
}

This causes some unexpected behavior when the EndRequest event executes. That event has code to add response headers (like correlation id, data center information, etc), which fails as the response headers are already written.

I checked the state of httpContext.Response.HeadersWritten and it gets set to true immediately after the call to httpContext.Response.Redirect(newUrl).

Don't want to rewrite my entire endrequest code every time I call redirect in the request pipeline. Is there a way to redirect without the headers being written until after the EndRequest event fires?

Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
  • Have you seen [this](https://stackoverflow.com/questions/4945883/how-to-redirect-http-to-https-in-mvc-application-iis7-5/4945950#4945950) answer? You could redirect to https in an action filter. – Hooman Bahreini Sep 18 '19 at 00:49
  • We have some requests not going through controllers, so cant really use a action filter. Thanks for the suggestion though. – Arnab Chakraborty Sep 18 '19 at 03:21

0 Answers0