HttpCachePolicy和@OutputCache指令。编程
对于输出缓存的控制,除了使用OutputCache指令外,还能够使用HttpCachePolicy类,经过该类能够编程控制缓存。浏览器
Response.Cache属性提供了System.Web.HttpCachePolicy类的一个实例。下面是使用@OutputCache指令和HttpCachePolicy之间等效的代码:缓存
<%@ OutputCache Duration="60" VaryByParam="None" %> 服务器
编程的方法: ide
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); Response.Cache.SetCacheability(HttpCacheability.Public); 代理
<%@ OutputCache Duration="60" Location="Client" VaryByParam="None" %> it
编程的方法: io
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); Response.Cache.SetCacheability(HttpCacheability.Private); class
<%@ OutputCache Duration="60" Location="Downstream" VaryByParam="None" %> stream
编程的方法:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.SetNoServerCaching();
<%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %>
编程的方法:
TimeSpan freshness = new TimeSpan(0,0,0,60); DateTime now = DateTime.Now; Response.Cache.SetExpires(now.Add(freshness)); Response.Cache.SetMaxAge(freshness); Response.Cache.SetCacheability(HttpCacheability.Server); Response.Cache.SetValidUntilExpires(true);
<%@ OutputCache duration="60" varybyparam="City" %>
编程的方法:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.VaryByParams["City"] = true;
VaryByCustom 属性、 VaryByHeader 属性和 @ OutputCache 指令中的 VaryByParam 属性,HttpCachePolicy 类提供了 VaryByHeaders 属性和 VaryByParams 属性和 SetVaryByCustom 方法。
要关闭输出缓存的 ASP.NET 网页在客户端的位置和代理的位置设置 位置 属性值为 无,而后 VaryByParam 值设置为 无@ OutputCache 指令中。使用下面的代码示例关闭客户端和代理服务器缓存。
<%@ OutputCache Location="None" VaryByParam="None" %>
Response.Cache.SetCacheability(HttpCacheability.NoCache);