The number one reason here to capture all HTML output during the life cycle of an ASPX page is to intercept any glaring HTML violations that Web Server controls currently produce. By default, we should find that ASP.NET server controls render something between XHTML and HTML 4.x which is effectively nothing that can be validated under any W3C standard.
This problem is discussed in detail here:
http://www.liquid-internet.co.uk/content/dynamic/pages/series1article1.aspx
The article "Valid XHTML within .NET" by Kevin Brown of Liquid Internet Limited is somewhat complex. A more brute force way of handling this is by overriding the Render() event of the base class of Page:
override protected void Render(HtmlTextWriter HTWriter)
{
StringBuilder StrBuilder = new StringBuilder();
StringWriter StrWriter = new StringWriter(StrBuilder);
HtmlTextWriter BaseHTWriter = new HtmlTextWriter(StrWriter);
/*
Capture all HTML output for this page
and flush into the underlying String Builder:
*/
base.Render(BaseHTWriter);
BaseHTWriter.Flush();
string HTML = StrBuilder.ToString();
HTML = HTML.Replace("foo","bar");
HTWriter.Write(HTML);
}
We can see that the string HTML is replacing any substring "foo" with "bar." Of course, more complex stuff can be done featuring Regular Expresssions. Thanks to Greg McCarty (gmccarty@bizmat.com) for the original design.