HTTP模块中的响应修改

发布于 2024-11-15 18:54:09 字数 513 浏览 2 评论 0 原文

我用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-11-22 18:54:09

您需要在发送标头之前修改响应。我从来没有这样做过,但我猜正确的事件是 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. link

Here'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文