Occasoinally i get this exception thrown (viewable in elmah)
System.Web.HttpException:
Server cannot append header after HTTP headers have been sent.
System.Reflection.TargetInvocationException: Exception has been thrown by the target
of an invocation. ---> System.Web.HttpException: Server cannot append header after
HTTP headers have been sent.
at System.Web.HttpHeaderCollection.SetHeader(String name, String value,
Boolean replace)
at System.Web.HttpHeaderCollection.Add(String name, String value)
at BettingOnYou.MvcApplication.Application_EndRequest() in /Global.asax.cs:line 55
This line corresponds to:
protected void Application_EndRequest()
{
// By default, IE renders pages on the intranet (same subnet) in compatibility mode.
// IE=edge forces IE to render in it's moststandard mode possible.
// IE8 Standards in IE8, IE9 Standardss in IE9, etc..
//
// Chrome=1 causes ChromeFrame to load, if installed. This allows IE6 to use
// a Chrome frame to render nicely.
Response.Headers.Add("X-UA-Compatible", "IE=edge, Chrome=1");
}
Is there a more appropriate place to do this? I know the EndRequest seems kind of odd, but every example of people using this places it here.
Application_BeginRequestwould be a better place to do it. Unless maybe you have some reason somewhere else in your application for waiting until the end, though I can’t think of a good reason in common cases.If you really want to keep it in
Application_EndRequest, you can turn on output buffering (Response.BufferOutput = true;somewhere early, like inPage_Load) so headers aren’t sent until the request is completely processed. There are pros and cons to output buffering though, so make sure you read up on that if you want to try it.