添加头时在end_request中抛出的异常

时间:2021-05-14 02:08:53

Occasoinally i get this exception thrown (viewable in elmah)

有时我会抛出这个异常(可在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.

有更合适的地方做这个吗?我知道遗书看起来有点奇怪,但是每个人使用它的例子都在这里。

2 个解决方案

#1


1  

Application_BeginRequest would 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.

Application_BeginRequest是更好的方法。除非你在你的应用程序中有什么理由等到最后,尽管在一般情况下我想不出一个好的理由。

If you really want to keep it in Application_EndRequest, you can turn on output buffering (Response.BufferOutput = true; somewhere early, like in Page_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.

如果您真的想将它保存在Application_EndRequest中,您可以打开输出缓冲(响应)。BufferOutput = true;在早期的某个地方,比如Page_Load),所以在请求被完全处理之前不会发送报头。不过,输出缓冲也有优缺点,所以如果您想尝试的话,一定要仔细阅读。

#2


0  

For the benefit of those landing here, there's another way of adding X-UA-Compatible (or any other such header). You can also include X-UA-Compatible as a meta tag in your master page OR default.cshtml like so:

对于那些在这里着陆的人来说,有另一种方法可以添加x - ua兼容(或任何其他类似的头)。您还可以将x - ua兼容作为您的主页或默认的元标记。cshtml一样:

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

This technique is the easiest to implement especially on shared hosting where you may not be able to tweak IIS settings too easily. A great discussion on * can be found here.

这种技术是最容易实现的,特别是在共享主机上,您可能无法太容易地调整IIS设置。关于*的讨论可以在这里找到。

#1


1  

Application_BeginRequest would 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.

Application_BeginRequest是更好的方法。除非你在你的应用程序中有什么理由等到最后,尽管在一般情况下我想不出一个好的理由。

If you really want to keep it in Application_EndRequest, you can turn on output buffering (Response.BufferOutput = true; somewhere early, like in Page_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.

如果您真的想将它保存在Application_EndRequest中,您可以打开输出缓冲(响应)。BufferOutput = true;在早期的某个地方,比如Page_Load),所以在请求被完全处理之前不会发送报头。不过,输出缓冲也有优缺点,所以如果您想尝试的话,一定要仔细阅读。

#2


0  

For the benefit of those landing here, there's another way of adding X-UA-Compatible (or any other such header). You can also include X-UA-Compatible as a meta tag in your master page OR default.cshtml like so:

对于那些在这里着陆的人来说,有另一种方法可以添加x - ua兼容(或任何其他类似的头)。您还可以将x - ua兼容作为您的主页或默认的元标记。cshtml一样:

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

This technique is the easiest to implement especially on shared hosting where you may not be able to tweak IIS settings too easily. A great discussion on * can be found here.

这种技术是最容易实现的,特别是在共享主机上,您可能无法太容易地调整IIS设置。关于*的讨论可以在这里找到。