21.6. Saving and Reusing HTML Output


To improve the performance of pages that rarely change, you want to capture the output of those pages and save it for reuse when those pages are requested.


Create the page that contains the desired content as you would any other page, including the server controls you need. At the end of the Page_Load method, use the RenderControl method of the Page control to generate the HTML, and save the HTML to a file.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Create an HtmlTextWriter to use for rendering the page.

  2. Use the RenderControl method of the Page control to render the output of the page to the HtmlTextWriter.

  3. Save the rendered output to a file and redirect to another page.

Examples 21-16 and 21-17 show the VB and C# code-behind files for our application that demonstrates this solution.


Occasionally, it’s beneficial to save the HTML output from a generated page. This is commonly done when using the saved HTML can significantly improve web site performance. If the content of a page is static, for example, there is no point in dynamically generating HTML each time the page is requested. Until the advent of ASP.NET, the only way to save the HTML was to use the “Save as Complete Web Page” feature of Internet Explorer or another browser. Though this method does save the HTML, it copies all of the page images to the local machine and changes the image references to point to the local copies. If you ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.