18.6. Saving and Reusing HTML Output
Problem
To improve the performance of pages that rarely change, you want to capture the output of those pages and save it for reuse when the page is requested.
Solution
Create the page that contains the desired content just 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 then save
the HTML to a file.
In the code-behind class for the page, use the .NET language of your choice to:
Create an
HtmlTextWriter
to use for rendering the page.Use the
RenderControl
method of thePage
control to render the output of the page to theHtmlTextWriter
.Save the rendered output to a file and redirect to another page.
Example 18-16 and Example 18-17 show the VB and C# code-behind files for our application that demonstrates this solution.
Discussion
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. Although this method does save the HTML, it also copies all of the page images to the local machine and changes the image references to point to ...
Get ASP.NET Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.