我如何知道HTTP头是在ASP中发送的。网络应用程序?

时间:2022-03-22 04:16:32

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:

长话短说,我有一个ASP。我正在调试的NET应用程序,在某些情况下,应用程序会在Response.Redirect()语句中抛出异常:

"Cannot redirect after HTTP headers have been sent."

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

我或多或少地得到了它,除了我不知道头是在哪里发送的。

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

在ASP中有什么需要查找的吗?将表明HTTP头已经发送的NET应用程序?

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.

奖金困难:ASP。NET应用程序仍然在。NET 1.1中。关于升级延迟的情况是一个非常棘手的问题。

2 个解决方案

#1


16  

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

HttpApplication有一个事件presendrequestheader,它在头是writtne时被调用。订阅它并记录它或添加断点。

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.

除此之外,HttpResponse有一个内部属性,称为headerswrite (. net 1.1中的_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.

在所有页面生命周期事件之前/之后检查此方法。一旦你知道哪个事件正在写头信息,就添加更多的头信息检查,以找出它们写在哪里。通过逐步缩小对该属性的检查,您将找到它。

New info

新信息

HeadersWritten property is public starting from .Net 4.5.2

HeadersWritten属性是从。net 4.5.2开始的公共属性

#2


5  

Samuel's reply just solved this problem for me (+1). I can't paste a code sample in a comment, but in the interest of helping others, here's how I used the event he suggested to add a HeadersWritten property to my IHTTPHandler:

塞缪尔的回答为我解决了这个问题(+1)。我无法将代码示例粘贴到注释中,但为了帮助其他人,以下是我如何使用他建议的事件向我的IHTTPHandler添加一个HeadersWritten属性:

protected bool HeadersWritten { get; private set; }

void ApplicationInstance_BeginRequest(object sender, EventArgs e)
{
    HeadersWritten = false;
}

void ApplicationInstance_PreSendRequestHeaders(object sender, EventArgs e)
{
    HeadersWritten = true;
}

public void ProcessRequest(HttpContextBase context)
{
    context.ApplicationInstance.PreSendRequestHeaders += new EventHandler(ApplicationInstance_PreSendRequestHeaders);
    do_some_stuff();
}

In my code that would break if I mess with headers too late, I simply check the HeadersWritten property first:

在我的代码中,如果我把页眉弄得太迟了会崩溃,我只需要先检查一下HeadersWritten属性:

if (!HeadersWritten)
{
    Context.Response.StatusDescription = get_custom_description(Context.Response.StatusCode);
}

#1


16  

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

HttpApplication有一个事件presendrequestheader,它在头是writtne时被调用。订阅它并记录它或添加断点。

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.

除此之外,HttpResponse有一个内部属性,称为headerswrite (. net 1.1中的_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.

在所有页面生命周期事件之前/之后检查此方法。一旦你知道哪个事件正在写头信息,就添加更多的头信息检查,以找出它们写在哪里。通过逐步缩小对该属性的检查,您将找到它。

New info

新信息

HeadersWritten property is public starting from .Net 4.5.2

HeadersWritten属性是从。net 4.5.2开始的公共属性

#2


5  

Samuel's reply just solved this problem for me (+1). I can't paste a code sample in a comment, but in the interest of helping others, here's how I used the event he suggested to add a HeadersWritten property to my IHTTPHandler:

塞缪尔的回答为我解决了这个问题(+1)。我无法将代码示例粘贴到注释中,但为了帮助其他人,以下是我如何使用他建议的事件向我的IHTTPHandler添加一个HeadersWritten属性:

protected bool HeadersWritten { get; private set; }

void ApplicationInstance_BeginRequest(object sender, EventArgs e)
{
    HeadersWritten = false;
}

void ApplicationInstance_PreSendRequestHeaders(object sender, EventArgs e)
{
    HeadersWritten = true;
}

public void ProcessRequest(HttpContextBase context)
{
    context.ApplicationInstance.PreSendRequestHeaders += new EventHandler(ApplicationInstance_PreSendRequestHeaders);
    do_some_stuff();
}

In my code that would break if I mess with headers too late, I simply check the HeadersWritten property first:

在我的代码中,如果我把页眉弄得太迟了会崩溃,我只需要先检查一下HeadersWritten属性:

if (!HeadersWritten)
{
    Context.Response.StatusDescription = get_custom_description(Context.Response.StatusCode);
}