How to intercept and modify ASP.NET Response

I need to intercept the response generated by ASP.NET pages immediately before they are sent to the client and possibly make changes to the response string.  Specifically, I want to have tokens in the page HTML that I can replace with dynamic values "on the fily".  I need this capability for the entire site, not just a single page.  Is there an Application-wide event I can trap (sort of like Render or PreRender) that I can use?  Also,how do I access the string itself?
GroupIII-GPhillipsAsked:
Who is Participating?
 
aibusinesssolutionsConnect With a Mentor Commented:
The problem the the Response Output Stream is that it is "Write" only, which means you can write all you want to it, but you cannot read it to replace anything.  There is a way around this though, which involves using a Response.Filter.

You could include the Response.Filter in the Application_PreSendRequestContent, which is fired just before the content is sent to the user, by doing something like this:
Response.Filter = New MyFilter(Response.Filter)

Here is a simple example for creating a filter class.
http://aspnetlibrary.com/articledetails.aspx?article=Use-Response.Filter-to-intercept-your-HTML



0
 
CtrlAltDlCommented:
Here is how you add a footer to each page using the Application_EndRequest event in the global.asax file:

protected void Application_EndRequest(Object sender, EventArgs e)
{
    Response.Write("Copyright 2002-" + dateStr );
    Response.Write("This is my customer footer that  from Application level" );
    Response.Write("<HR>");
}

I guess you could probably replace any tokens like this (untested):
Context.Response.ToString().Replace("%token%", "my new text");
0
 
GroupIII-GPhillipsAuthor Commented:
That is exactly what was needed.  To add one thing, the solution on the referenced site neede a ref on every page.  I put the same code in the Application_AcquireRequestState in Global.asax and it did the whole site without having to change every page.  Great job - Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.