[英]Server cannot append header after HTTP headers have been sent
I get this exception intermittently in my asp.net c# web application:我在我的 asp.net c# web 应用程序中间歇性地收到此异常:
Server cannot append header after HTTP headers have been sent.在发送 HTTP 标头后,服务器无法 append header。
And it caused by the application appending content to the pages response header after the page has been sent.它是由应用程序在页面发送后将内容附加到页面响应 header 引起的。 I am not sure why its intermittent but what I need to do is perform a check prior to the modification for the headers to check if the page has been sent.我不确定为什么它是间歇性的,但我需要做的是在修改标题之前执行检查以检查页面是否已发送。
Anyone know how I can achieve such a check?任何人都知道我怎样才能实现这样的检查?
There are 2 ways to do it:有两种方法可以做到:
Subscribe to the PreSendRequestHeaders
of HttpApplication
and assume that the headers have been sent at this point.订阅HttpApplication
的PreSendRequestHeaders
并假设此时标头已发送。 Set a flag on the context and check for it everywhere在上下文上设置一个标志并在任何地方检查它
The ugly solution: HttpResponse
has a internal property called HeadersWritten
.丑陋的解决方案: HttpResponse
有一个名为HeadersWritten
的内部属性。 Since it's internal you'll have to access it through reflection.由于它是内部的,因此您必须通过反射来访问它。 I'd recommand only to use this for debugging.我建议仅将其用于调试。 Check it before/after all the page lifecyle events and find out where the problem is.在所有页面生命周期事件之前/之后检查它并找出问题所在。 Don't leave this in production code不要将其留在生产代码中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.