我用 C# 构建了一个 http 模块,它仅覆盖生命周期的 EndRequest,检查响应标头的状态代码并根据需要修改响应代码。例如:
HttpContext context = ((HttpApplication)source).Context;
if (context.Response.StatusCode == 200)
{
context.Response.StatusCode = 404;
}
当我针对本地主机(真正的 IIS)测试它时,这似乎对我来说工作得很好,但是当我启用 Intranet 访问并从另一台计算机进行测试时,它每次都会失败。
当我通过另一台计算机进行测试时看到的错误是:
“发送 HTTP 标头后服务器无法设置状态。”
我还注意到,请求同一个文件两次,而在本地它只请求该文件一次。我听到有人说了一些关于输出缓冲的事情,但我也尝试将 BeginRequest 响应 OutputBuffer 设置为 true 并得到了相同的结果。
想法?
I built an http module in C# that just overrides the EndRequest of a lifecycle, checks the status code of the response header and modifies the response code it if needs be. Something like:
HttpContext context = ((HttpApplication)source).Context;
if (context.Response.StatusCode == 200)
{
context.Response.StatusCode = 404;
}
This seems to work fine for me when I test it against localhost (real IIS), but when I enable intranet access and test from another computer it fails every time.
The error I am seeing when I test via another computer is:
"Server cannot set status after HTTP headers have been sent."
I also noticed that requests the same file twice, whereas locally it only requests the file once. I heard someone say something about output buffering, but I also tried setting the on BeginRequest response OutputBuffer to true and got the same results.
Thoughts?
发布评论
评论(1)
您需要在发送标头之前修改响应。我从来没有这样做过,但我猜正确的事件是
PreSendRequestHeaders
。 链接<一href="http://www.google.com/codesearch#kZG2CHKIKf8/trunk/1.0a/OAuth.Net.ServiceProvider/Authentication/OAuthPipelineModule.cs&q=PreSendRequestHeaders%20lang%3a%5Ec#%24&type=cs " rel="noreferrer">这是在此事件中设置标头的示例。
如果此事件不适合您,请检查管道中处理程序后处理后的一些事件:http://blogs.msdn.com/b/carloc/archive/2007/12/19/application-page-and-control-lifecycle.aspx 不过要小心,因为您可能必须为 IIS 7 集成管道重写代码。
You need to modify the response before the headers are sent. I've never had to do this, but I would guess the correct event would be
PreSendRequestHeaders
. linkHere's an example of setting headers in this event.
If this event doesn't work for you, check out some events after the handler's post process in the pipeline: http://blogs.msdn.com/b/carloc/archive/2007/12/19/application-page-and-control-lifecycle.aspx Be careful, though, because you may have to rewrite your code for the IIS 7 integrated pipeline.