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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0