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?