Capture HTML page outptut in ASP.NET

by Marius Gheorghe 17. April 2006 21:16

Sometimes you need to capture the page HTML output (for logging purposes or to mail it). The easiest way to do it is to override the Render event handler like this :

protected override void Render(HtmlTextWriter writer)

{

      //create a new string builder

      StringBuilder builder = new StringBuilder();

 

      //create a StringWriter based on the builder

      StringWriter sw= new StringWriter(builder);

  

      //create a new HmtlTextWriter based on the string writer

      HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);

 

      //call the base Page implementation which generates the HTML

      base.Render(htmlWriter);

 

      //get the output from the string

      string html = builder.ToString();

  

     //do usefull stuff

}

 

Tags:

dotNET

Comments are closed

marius gheorghe

developer, dad, gamer

Contact me

My Resume

Favourite Tools