?
Solved

How to intercept and modify ASP.NET Response

Posted on 2009-02-11
3
Medium Priority
?
408 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:GroupIII-GPhillips
3 Comments
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 23618311
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
 
LVL 15

Accepted Solution

by:
aibusinesssolutions earned 500 total points
ID: 23628570
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
 

Author Closing Comment

by:GroupIII-GPhillips
ID: 31545879
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question