ASP.NET Capture and replace output in Global.asax

拟墨画扇 提交于 2019-12-11 01:48:26

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/819326/asp-net-capture-and-replace-output-in-global-asax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!