【问题标题】:ASP.NET Capture and replace output in Global.asaxASP.NET 在 Global.asax 中捕获和替换输出
【发布时间】:2009-05-04 08:51:28
【问题描述】:

我需要替换从我网站上的每个页面发送的一些数据,我想用 Global.asax 来做。到目前为止,这是我尝试过的:

void Application_PreSendRequestContent(object sender, EventArgs e)
{
    System.IO.StreamReader sr = new System.IO.StreamReader(Response.OutputStream);
    String output = sr.ReadToEnd();

    Response.ClearContent();
    Response.Write("Testing..");
}

但这给了我一个 ArgumentException。我究竟做错了什么?有没有更好的方法来做到这一点?

谢谢

【问题讨论】:

  • 哪一行给出了 ArgumentException?异常中是否有更详细的消息?
  • StreamReader-constructor 抛出 ArgumentException 说它无法读取流。我也尝试在 Application_BeginRequest-event 中执行此操作以及从 Response.Filter 读取。
  • 修改的正确方法是通过响应过滤器。您可以创建一个自定义流,通过它您可以修改实际响应。您可以在此页面中查看工作示例davidsekar.com/asp-net/…

标签: c# asp.net global-asax


【解决方案1】:

有没有更好的方法来做到这一点?

HttpModule 可能是此类任务的更好选择。

有关如何修改请求响应的示例,请查看这篇文章:Producing XHTML-Compliant Pages With Response Filters

【讨论】:

  • 感谢您的回答,我会调查一下。
【解决方案2】:

Logging raw HTTP request/response in ASP.NET MVC & IIS7 帖子很好地描述了如何获取响应的副本。

【讨论】:

    猜你喜欢
    • 2011-06-25
    • 2021-11-27
    • 1970-01-01
    • 2011-06-16
    • 2013-02-16
    • 1970-01-01
    • 2021-02-13
    • 2011-02-24
    • 2011-09-25
    相关资源
    最近更新 更多