ASP.NET Capture and replace output in Global.asax

2019-07-10 13:56发布

I need to replace some data that's sent from every page on my site, and I think doing it with Global.asax. This is what I have tried with so far:

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..");
}

But this gives me an ArgumentException. What am I doing wrong? Is there any better way to do this?

Thanks

2条回答
虎瘦雄心在
2楼-- · 2019-07-10 14:21

Is there any better way to do this?

A HttpModule might be the better choice for such a task.

For an example on how to modify the response of a request, have a look at this article: Producing XHTML-Compliant Pages With Response Filters.

查看更多
相关推荐>>
3楼-- · 2019-07-10 14:35

The post Logging raw HTTP request/response in ASP.NET MVC & IIS7 descibes very nicely exactly how to get a copy of the response.

查看更多
登录 后发表回答