Microsoft KB Archive/315896

= HOW TO: Improve Performance by Caching Pages in ASP.NET =

Article ID: 315896

Article Last Modified on 5/31/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q315896



IN THIS TASK
SUMMARY
 * Requirements
 * Description of the Technique
 * Create an ASP.NET Web Application That Uses @ OutputCache
 * Complete Code Listing
 * Verify That It Works

REFERENCES



SUMMARY
This article demonstrates how to improve the performance of ASP.NET applications by caching entire ASP.NET pages with the @ OutputCache page directive. You can also the @ OutputCache page directive to cache specific portions of an ASP.NET page, though this article does not demonstrate this implementation.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET
 * Microsoft Internet Information Server (IIS) 4.0 or Microsoft Internet Information Services (IIS) 5.0

This article assumes that you are familiar with the following topics:
 * Web applications
 * Microsoft ASP.NET

back to the top

Description of the Technique
When you cache information, you place the information in memory. Normally, when IIS receives a request for a page, IIS reads the page from the hard disk and then sends the page. Sometimes, IIS can automatically store information in memory to improve performance. The @ OutputCache directive allows you to hold a page in memory, even if that page does not fit the criteria that IIS uses to determine what information it holds in memory.

For example, pages that access a database are run again each time you browse to the page to obtain the latest data. When you use the @ OutputCache directive, the page is held in memory with the results of one query to the database until the specified time period expires. Therefore, you can use the @ OutputCache directive to avoid querying the database every time someone browses to the page. This is very useful for data that does not change frequently.

back to the top

Create an ASP.NET Web Application That Uses @ OutputCache
In this section, you create an ASP.NET Web application that uses the @ OutputCache page directive to cache the page for a specific time period.  Follow these steps to create a new ASP.NET Web application in Visual Basic .NET:  Open Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.   Switch to HTML view in the WebForm1.aspx window, and then add the following directive immediately after the @ Page directive: <%@ OutputCache Duration=&quot;10&quot; VaryByParam=&quot;none&quot; %> This code sets the duration attribute of the @ OutputCache directive to 10 seconds so that the page contents are cached for 10 seconds. In addition, the VaryByParam attribute consists of a semicolon-separated list of strings that you can use to vary the output cache based on variables that the user defines. This functionality is beyond the scope of this article, so the preceding code sets VaryByParam to none.   In the HTML view of WebForm1.aspx, add the following code between the opening and closing tags to add a Label control to the form: <asp:Label> Last cached: <%Response.Write(Now)%> </asp:Label> You use this label to display the last time that the page was loaded. The value in this label should only change once every 10 seconds, regardless how many times the page is refreshed. </li> On the File menu, click Save.</li> On the Build menu, click Build Solution.</li> Right-click the page, and then click View In Browser. Notice that a label appears, which displays the current time.</li> Refresh the page multiple times. Notice that the label changes only after the 10-second time period expires.</li></ol>

back to the top

Complete Code Listing
<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;WebApplication1.WebForm1&quot;%> <%@ OutputCache Duration=&quot;10&quot; VaryByParam=&quot;none&quot; %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> WebForm1 <meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio.NET 7.0&quot;> <meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic 7.0&quot;> <meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;> <meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:Label> Last Cached: <%Response.Write(Now)%> </asp:Label> NOTE: You may need to modify the Inherits attribute in the following line of code as necessary to reference the name of your project and Web Form: <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;WebApplication1.WebForm1&quot;%> The code currently assumes that your project is named WebApplication1 and that your Web Form is named WebForm1.

back to the top

Verify That It Works

 * 1) Open multiple browser windows.
 * 2) In each window, browse to your .aspx page as though you were an end user. Notice that all of the windows display the same time for the 10-second duration, even though you did not browse to the page at the same time.
 * 3) After 10 seconds, browse to or refresh your .aspx page. Notice that all of the windows update the time.

back to the top

<div class="references_section">