Microsoft KB Archive/811431: Difference between revisions
(importing KB archive) |
m (Text replacement - "<" to "<") |
||
Line 95: | Line 95: | ||
Declarative Approach:<br /> | Declarative Approach:<br /> | ||
</p> | </p> | ||
<pre class="codesample"> | <pre class="codesample"><%@ OutputCache Duration="60" VaryByParam="None" %></pre> | ||
<p><br /> | <p><br /> | ||
Programmatic Approach:<br /> | Programmatic Approach:<br /> | ||
Line 105: | Line 105: | ||
Declarative Approach:<br /> | Declarative Approach:<br /> | ||
</p> | </p> | ||
<pre class="codesample"> | <pre class="codesample"><%@ OutputCache Duration="60" Location="Client" VaryByParam="None" %></pre> | ||
<p><br /> | <p><br /> | ||
Programmatic Approach:<br /> | Programmatic Approach:<br /> | ||
Line 115: | Line 115: | ||
Declarative Approach:<br /> | Declarative Approach:<br /> | ||
</p> | </p> | ||
<pre class="codesample"> | <pre class="codesample"><%@ OutputCache Duration="60" Location="Downstream" VaryByParam="None" %></pre> | ||
<p><br /> | <p><br /> | ||
Programmatic Approach:<br /> | Programmatic Approach:<br /> | ||
Line 126: | Line 126: | ||
Declarative Approach:<br /> | Declarative Approach:<br /> | ||
</p> | </p> | ||
<pre class="codesample"> | <pre class="codesample"><%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %></pre> | ||
<p><br /> | <p><br /> | ||
Programmatic Approach:<br /> | Programmatic Approach:<br /> | ||
Line 136: | Line 136: | ||
Declarative Approach:<br /> | Declarative Approach:<br /> | ||
</p> | </p> | ||
<pre class="codesample"> | <pre class="codesample"><%@ OutputCache duration="60" varybyparam="City" %></pre> | ||
<p><br /> | <p><br /> | ||
Programmatic Approach:<br /> | Programmatic Approach:<br /> | ||
Line 151: | Line 151: | ||
<ul> | <ul> | ||
<li><p>Declarative Approach:</p> | <li><p>Declarative Approach:</p> | ||
<pre class="codesample"> | <pre class="codesample"><%@ OutputCache Location="None" VaryByParam="None" %></pre></li> | ||
<li><p>Programmatic Approach:</p> | <li><p>Programmatic Approach:</p> | ||
<pre class="codesample">Response.Cache.SetCacheability(HttpCacheability.NoCache)</pre></li></ul> | <pre class="codesample">Response.Cache.SetCacheability(HttpCacheability.NoCache)</pre></li></ul> | ||
Line 163: | Line 163: | ||
<li>By default, '''WebForm1.aspx''' is created.</li> | <li>By default, '''WebForm1.aspx''' is created.</li> | ||
<li><p>In the HTML view of '''WebForm1.aspx''', replace the existing code with the following sample code:</p> | <li><p>In the HTML view of '''WebForm1.aspx''', replace the existing code with the following sample code:</p> | ||
<pre class="codesample"> | <pre class="codesample"><%@ Import Namespace="System.Data.SqlClient" %> | ||
<%@ Import Namespace="System.Data" %> | |||
<HTML> | |||
<script language="vb" runat="server"> | |||
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) | Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) | ||
Line 198: | Line 198: | ||
End Sub | End Sub | ||
</script> | |||
<body> | |||
<form method="GET" runat="server" ID="Form1"> | |||
<h3><font face="Verdana">Caching Data</font></h3> | |||
<ASP:DataGrid id="MyDataGrid" runat="server" | |||
Width="700" | Width="700" | ||
BackColor="#ccccff" | BackColor="#ccccff" | ||
Line 212: | Line 212: | ||
Font-Size="8pt" | Font-Size="8pt" | ||
HeaderStyle-BackColor="#aaaad" /> | HeaderStyle-BackColor="#aaaad" /> | ||
<p> | |||
<i><asp:label id="CacheMsg" runat="server"/></i> | |||
</form> | |||
</P> | |||
</body> | |||
</HTML></pre> | |||
<p>'''Note''' Replace the values for <code>ServerName</code>, <code>UID</code>, and <code>PWD</code> in the sample code for the '''SqlConnection''' object with your SQL Server Name, User ID, and Password.</p></li> | <p>'''Note''' Replace the values for <code>ServerName</code>, <code>UID</code>, and <code>PWD</code> in the sample code for the '''SqlConnection''' object with your SQL Server Name, User ID, and Password.</p></li> | ||
<li>On the '''Debug''' menu, click '''Start''' to run the application.<br /> | <li>On the '''Debug''' menu, click '''Start''' to run the application.<br /> |
Revision as of 09:26, 21 July 2020
Article ID: 811431
Article Last Modified on 5/31/2007
APPLIES TO
- Microsoft ASP.NET 1.1
- Microsoft ASP.NET 1.0
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
For a Microsoft Visual C# .NET version of this article, see 323290.
CONTENTS
SUMMARY
This step-by-step article describes ways to control how ASP.NET caches Web pages and data objects. By caching, you avoid re-creating information when you make later requests. Caching is an important technique for building high performance and scalable server applications. In the first request for the page, you can store data objects, pages, or parts of the page in the memory. You can store these items on the Web server, on the proxy server, or on the browser.
back to the top
MORE INFORMATION
ASP.NET provides convenient methods to control caching. You can use the @ OutputCache directive to control page output caching in ASP.NET. Use HttpCachePolicy class to store arbitrary objects, such as datasets, to the server memory. You can store the cache in applications such as the client browser, the proxy server, and Microsoft Internet Information Services (IIS). By using Cache-Control HTTP Header, you can control caching.
For additional information about ASP.NET output caching, click the following article number to view the article in the Microsoft Knowledge Base:
308516 How To Control Page Output Caching in ASP.NET by Using Visual Basic .NET
Cache ASP.NET Pages
You can cache by using the @ OutputCache directive, or programmatically through code by using Visual Basic .NET or Visual C# .NET. The @ OutputCache directive contains a Location attribute. This attribute determines the location for the cached item. You may specify one of the following locations:
- Any - This stores the output cache in the client's browser, on the proxy server (or any other server) that participates in the request, or on the server where the request is processed. By default, Any is selected.
- Client - This stores output cache on the client's browser.
- Downstream - This stores the output cache in any cache-capable devices (other than the origin server) that participate in the request.
- Server - This stores the output cache on the Web server.
- None - This turns off the output cache.
The following are code samples for the @ OutputCache directive and equivalent programmatic code.
To store the output cache for a specified duration
Declarative Approach:
<%@ OutputCache Duration="60" VaryByParam="None" %>
Programmatic Approach:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)) Response.Cache.SetCacheability(HttpCacheability.Public)
To store the output cache on the browser client where the request originated
Declarative Approach:
<%@ OutputCache Duration="60" Location="Client" VaryByParam="None" %>
Programmatic Approach:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)) Response.Cache.SetCacheability(HttpCacheability.Private)
To store the output cache on any HTTP 1.1 cache-capable devices including the proxy servers and the client that made request
Declarative Approach:
<%@ OutputCache Duration="60" Location="Downstream" VaryByParam="None" %>
Programmatic Approach:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)) Response.Cache.SetCacheability(HttpCacheability.Public) Response.Cache.SetNoServerCaching()
To store the output cache on the Web server
Declarative Approach:
<%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %>
Programmatic Approach:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)) Response.Cache.SetCacheability(HttpCacheability.Server)
To cache the output for each HTTP request that arrives with a different City
Declarative Approach:
<%@ OutputCache duration="60" varybyparam="City" %>
Programmatic Approach:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)) Response.Cache.SetCacheability(HttpCacheability.Public) Response.Cache.VaryByParams("City") = true
For the VaryByCustom attribute, the VaryByHeader attribute, and the VaryByParam attribute in the @ OutputCache directive, the HttpCachePolicy class provides the VaryByHeaders property and the VaryByParams property, and the SetVaryByCustom method.
Turn Off Client and Proxy Caching
To turn off the output cache for an ASP.NET Web page at the client location and at the proxy location, set the Location attribute value to none, and then set the VaryByParam value to none in the @ OutputCache directive. Use the following code samples to turn off client and proxy caching.
Declarative Approach:
<%@ OutputCache Location="None" VaryByParam="None" %>
Programmatic Approach:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Cache Arbitrary Objects in Server Memory
ASP.NET includes a powerful, easy-to-use caching mechanism that you can use to store objects that require a lot of server resources to create in memory. The Cache class implements this method. Instances are private to each application and the lifetime is tied to the corresponding application. To cache the arbitrary objects in ASP.Net by using the Cache class, follow these steps:
- Create a new ASP.NET Web Application by using Visual Basic .NET.
- By default, WebForm1.aspx is created.
In the HTML view of WebForm1.aspx, replace the existing code with the following sample code:
<%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Data" %> <HTML> <script language="vb" runat="server"> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim Source As DataView 'Retrieve the DataView object from the Cache class. If it does not exist, add the DataView object to the Cache class. Source = CType(Cache("MyDataSet"), DataView) If Source Is Nothing Then Dim myConnection As SqlConnection = New SqlConnection("Server=ServerName; database=Pubs; user id=UID; password=PWD;") Dim myCommand As SqlDataAdapter = New SqlDataAdapter("select * from Authors", myConnection) Dim ds As DataSet = New DataSet() myCommand.Fill(ds, "Authors") Source = New DataView(ds.Tables("Authors")) Cache("MyDataSet") = Source CacheMsg.Text = "Dataset created explicitly" Else CacheMsg.Text = "Dataset retrieved from cache" End If ' Bind the DataView object to DataGrid. MyDataGrid.DataSource = Source MyDataGrid.DataBind() End Sub </script> <body> <form method="GET" runat="server" ID="Form1"> <h3><font face="Verdana">Caching Data</font></h3> <ASP:DataGrid id="MyDataGrid" runat="server" Width="700" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding=3 CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaad" /> <p> <i><asp:label id="CacheMsg" runat="server"/></i> </form> </P> </body> </HTML>
Note Replace the values for
ServerName
,UID
, andPWD
in the sample code for the SqlConnection object with your SQL Server Name, User ID, and Password.- On the Debug menu, click Start to run the application.
Note When you restart the application, the Cached object is re-created.
REFERENCES
For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
234067 How To Prevent Caching in Internet Explorer
247404 How to Modify the Cache-Control HTTP Header When You Use IIS
311006 How To Prevent Web Caching in Windows 2000
247389 IIS: How to Disable Caching of Specific MIME Types
313561 How To Set HTTP Headers for Content Expiration in IIS
For more information, visit the following Microsoft Web site:
Keywords: kbhowtomaster kbdatabinding kbcaching kbwebforms kbinfo KB811431