Location="Any | Client | Downstream | Server | None | ServerAndClient "
Shared="True | False"
VaryByControl="controlname"
VaryByCustom="browser | customstring"
VaryByHeader="headers"
VaryByParam="parametername"
VaryByContentEncoding="encodings"
CacheProfile="cache profile name | ''"
NoStore="true | false"
SqlDependency="database/table name pair | CommandNotification"
%>
Duration
, VaryByCustom
, VaryByParam
, 和VaryByControl属性来缓存我们的页面输出
,对于其它属性,您可以参考:http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OutPutCacheWithDuration</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="btnPostBack" runat="server" Text="Post Back" />
<p>
The page will be cached 10s, and then you can
click Button to update datetime.
</p>
</div>
</form>
</body>
</html>
OutputCache Duration="1000" VaryByControl="ddlOption".
运行ASP.NET Web应用程序和启动这个页面,我们可以看到,不同item有其相应的缓存。
代码<%@ OutputCache Duration="1000" VaryByControl="ddlOption" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OutPutCacheWithVaryByControl</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server"></asp:Label>
<br />
<br />
<asp:DropDownList ID="ddlOption" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlOption_SelectedIndexChanged">
<asp:ListItem Selected="True">Option One</asp:ListItem>
<asp:ListItem>Option Two</asp:ListItem>
<asp:ListItem>Option Three</asp:ListItem>
</asp:DropDownList>
<p>
The page will be rendered from cache basing
on the selected item of DropDownList.
The different item has corresponding cache.
</p>
</div>
</form>
</body>
</html>
3、VaryByCustom属性OutputCache Duration="1000"VaryByCustom="browser"VaryByParam="none"。
运行ASP.NET Web应用程序和使用IE和Firefox(不同的浏览器和版本)启动这个页面,我们将看到,对于不同的浏览器,缓存是不同的。
代码<%@ OutputCache Duration="1000" VaryByCustom="browser" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OutPutCacheWithVaryByCustom</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="btnPostBack" runat="server" Text="Post Back" />
<p>
The page will be rendered from cache basing
on the version of browser, such as IE and FireFox.
</p>
</div>
</form>
</body>
</html>
4、VaryByParam属性OutputCache Duration="1000" VaryByParam="id"。
~/OutputCacheWithParam.aspx?id=2 >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OutPutCacheWithVaryByParam</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server"></asp:Label>
<p>
The page will be rendered from cache until
the value of QueryString named "id" is
changed or Duration is expiration.
</p>
</div>
</form>
</body>
</html>
出处:http://zhuqil.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。