如何在ASP.NET应用程序中发送HTTP标头时如何判断? [英] How can I tell when HTTP Headers have been sent in an ASP.NET application?

查看:126
本文介绍了如何在ASP.NET应用程序中发送HTTP标头时如何判断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我有一个ASP.NET应用程序,我正在尝试调试,在某些情况下,在非常特殊的情况下,应用程序将在 Response.Redirect()说明:

Long story short, I have an ASP.NET application I'm trying to debug and at some point, in very particular circumstances, the application will throw exceptions at a Response.Redirect() stating:

"Cannot redirect after HTTP headers have been sent."

我或多或少得到了,除了我无法弄明白 where 标题已发送。

Which I more or less get, except that I cannot figure out where the headers were sent.

在ASP.NET应用程序中是否需要查找表明HTTP标头已发送的内容?

Is there something to look for in an ASP.NET application that will indicate that the HTTP headers have been sent?

BONUS DIFFICULTY: ASP.NET应用程序仍在.NET 1.1中。关于升级延迟的情况是一个非常痛苦的主题。

BONUS DIFFICULTY: The ASP.NET app is still in .NET 1.1. The circumstances regarding the delay behind the upgrade are a really sore subject.

推荐答案

HttpApplication 有一个事件 PreSendRequestHeaders ,只有在标题写入时才会调用。订阅并登录或添加断点。

HttpApplication has an event PreSendRequestHeaders which is called just when headers are writtne. Subscribe to this and log it or add a breakpoint.

除此之外, HttpResponse 有一个内部属性名为 HeadersWritten (.NET 1.1中的 _headersWritten 字段)。由于它是内部的,你不能直接访问它,但你可以通过反射。如果这仅用于内部调试(即非生产代码),则可以使用反射。

Beyond that, HttpResponse has a internal property called HeadersWritten (_headersWritten field in .NET 1.1). Since it's internal you can't access it directly, but you can through reflection. If this is only for internal debugging (i.e., not production code), then it's be fine to use reflection.

在所有页面生命周期事件之前/之后检查此方法。一旦知道哪个事件正在写出标题,添加更多 HeadersWritten 检查以找出它们写入的位置。通过逐步缩小支票到这家酒店,你会找到它。

Check this method before/after all the page lifecylce events. Once you know which event is writing out the headers, add more HeadersWritten checks to find out where they're getting written. Through progressive narrowing of checks to this property, you'll find it.

新信息

HeadersWritten 属性是从.Net 4.5.2开始公开的。

HeadersWritten property is public starting from .Net 4.5.2

这篇关于如何在ASP.NET应用程序中发送HTTP标头时如何判断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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