Server cannot append header after HTTP headers have been sent

只谈情不闲聊 提交于 2019-12-05 22:21:41

问题


I get this exception intermittently in my asp.net c# web application:

Server cannot append header after HTTP headers have been sent.

And it caused by the application appending content to the pages response header after the page has been sent. 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?


回答1:


There are 2 ways to do it:

  1. Subscribe to the PreSendRequestHeaders of HttpApplication and assume that the headers have been sent at this point. Set a flag on the context and check for it everywhere

  2. The ugly solution: HttpResponse has a internal property called 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



来源:https://stackoverflow.com/questions/6328437/server-cannot-append-header-after-http-headers-have-been-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!