添加头时抛出异常的END_REQUEST [英] Exception thrown in end_request when adding headers

查看:306
本文介绍了添加头时抛出异常的END_REQUEST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Occasoinally我得到这个异​​常抛出(可视在ELMAH)

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

这行对应:

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");
}

有没有更合适的地方来做到这一点?我知道EndRequest似乎有点古怪,但用这个人的每一个示例将在这里。

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_BeginRequest 将是一个更好的地方去做。除非也许你有某种原因,别的地方在你的应用程序等待,直到结束,但我想不出在普通情况下,一个很好的理由。

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_EndRequest ,您可以打开输出缓冲( Response.BufferOutput = TRUE; 早的地方,像的Page_Load ),所以标题不发送,直到请求被完全处理。有利弊输出缓冲的,所以确保你的,如果你想尝试读了它。

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.

这篇关于添加头时抛出异常的END_REQUEST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆