Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Capture and replace output in Global.asax

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

like image 662
alexn Avatar asked Oct 23 '25 15:10

alexn


1 Answers

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.

like image 138
M4N Avatar answered Oct 26 '25 08:10

M4N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!