有时我在生产环境中遇到异常:
- Process information
- Process ID: 3832
- Process name: w3wp.exe
- Account name: NT AUTHORITY\NETWORK SERVICE
- Exception information
- Exception type: System.Web.HttpException
- Exception message: Server cannot set status after HTTP headers have been sent.
- Request information
- Request URL: http://www.myulr.pl/logon
- Request path: /logon
- User host address: 10.11.9.1
- User: user001
- Is authenticated: True
- Authentication Type: Forms
- Thread account name: NT AUTHORITY\NETWORK SERVICE
- Thread information
- Thread ID: 10
- Thread account name: NT AUTHORITY\NETWORK SERVICE
- Is impersonating: False
Stack trace: at System.Web.HttpResponse.set_StatusCode(Int32 value) at
System.Web.HttpResponseWrapper.set_StatusCode(Int32 value) at
System.Web.Mvc.HandleErrorAttribute.OnException(ExceptionContext filterContext) at
System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList(1) filters, Exception exception) at
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at
System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4() at
System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0() at
System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8(1).<BeginSynchronous>b__7(IAsyncResult _) at
System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult(1).End() at
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& ompletedSynchronously)
我在我的测试环境中没有注意到这个错误,我应该检查什么?
我正在使用 ASP.NET MVC 2(发布候选版本 2)
最佳答案
我会大体上同意 Vagrant 的观点:
- 您的操作正在执行,将标记写入响应流
- 流是无缓冲的,强制响应 header 在标记写入开始之前写入。
- 您的 View 遇到运行时错误
- 异常处理程序开始尝试将状态代码设置为非 200 的其他内容
- 失败,因为标题已经发送。
我不同意 Vagrant 的地方是“不会导致绑定(bind)错误”补救措施——您仍然可能在 View 绑定(bind)中遇到运行时错误,例如空引用异常。
一个更好的解决方案是确保 Response.BufferOutput = true;
在任何字节被发送到响应流之前。例如在您的 Controller 操作或应用程序中的 On_Begin_Request 中。这使服务器传输、设置 cookie/ header 等成为自然结束响应或调用 end/flush 的正确方式。
当然还要检查缓冲区是否没有被刷新/在堆栈的更下方设置为 false。
MSDN 引用: HttpResponse.BufferOutput
关于asp.net-mvc - 发送 HTTP header 后服务器无法设置状态 IIS7.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2383169/