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
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.
The post Logging raw HTTP request/response in ASP.NET MVC & IIS7 descibes very nicely exactly how to get a copy of the response.