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

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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

Cheers,
 
S

Scott Allen

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,
 
D

Dennis Myrén

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.
 
D

Dr. StrangeDub

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
================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top