Capturing ASP.Net generated HTML to a file on web server

  • Thread starter Thread starter Dr. StrangeDub
  • Start date Start date
D

Dr. StrangeDub

I am looking for a way to capture the HTML file generated by an
ASP.Net application (just as is sent back to the client) and save it
to a designated spot on the web server. Here's a bit of background:
our application has a Report function that dynamically creates a new
page containing various tables of real-time data (through the C# code
"behind" the .aspx file). We have a new requirement to add a file
save function to this report page. So, I'd like to capture the final
HTML within the the.aspx file (exactly as is sent to the browser) and
save it (with an .htm extension) on the web server. Think of it as
having the ability to do a "view source" of the final HTML, and then
saving the source back to a file on the web server. Does anyone know
of supported way to capture the generated HTML and save it
server-side? All suggestions welcome and much appreciated.

Michael Rose
Unisys Corp.
 
Hi,

All you have to do is using a WebRequest instance to request that very same
page and later save the stream.

Cheers,
 
Hi Michael:

I'm not sure if you want to save the HTML from inside the ASP.NET
process or request the HTML from the web server inside a windows form.

Inside ASP.NET you can Server.Execute a page to get the HTML. From a
WinForm app you can use WebClient or WebRequest.

I have some code here:
http://odetocode.com/Articles/162.aspx

HTH,
 
The Render* methods of the
System.Web.UI.HtmlControls.HtmlControl/System.Web.UI.WebControls.WebControl
may be called manually to stream the HTML to a TextWriter.
 
Thanks to Scott, Ignacio, and Dennis for their replies. I pretty much
went with Scott's suggested use of Server.Execute. This turned out to
be quite easy. I haven't tweaked the code yet, but these five lines of
code esssentially did the job:

m_OutputFile = "c:\myDir\myFile.htm";
StreamWriter sw = new StreamWriter(m_OutputFile,true);
Server.Execute("ReportWindow.aspx", sw);
sw.Flush();
sw.Close();

Michael Rose
================
 
Back
Top