HttpCachePolicy.SetValidUntilExpires(Boolean) 方法

定义

指定 ASP.NET 缓存是否应忽略客户端发送的使缓存失效的 HTTP Cache-Control 标头。

public:
 void SetValidUntilExpires(bool validUntilExpires);
public void SetValidUntilExpires (bool validUntilExpires);
member this.SetValidUntilExpires : bool -> unit
Public Sub SetValidUntilExpires (validUntilExpires As Boolean)

参数

validUntilExpires
Boolean

如果缓存忽略 true 无效标头,则为 Cache-Control;否则为 false

示例

下面的代码示例演示如何使用 SetValidUntilExpires 该方法指示客户端发送的任何缓存无效标头将被忽略。

<!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>
    <title>ASP.NET Example</title>
<script language="C#" runat="server">

// The following example demonstrates the SetValidUntilExpires method of the
// HttpCachePolicy class. The SetValidUntilExpires method is set to true so 
// that the ASP.NET cache ignores the HTTP Cache-Control headers and the page 
// remains in the cache until it expires.

void Page_Load(object Sender, EventArgs e)
{
   // Set the expiration time for the page.
   Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
   // Set the VaryByHeaders attribute with the value Accept-Language to true.
   Response.Cache.VaryByHeaders["Accept-Language"] = true;
   // ASP.NET ignores cache invalidation headers and the page remains in 
   // the cache until it expires.
   Response.Cache.SetValidUntilExpires(true);
   Response.Write("The SetValidUntilExpires method is set to true and ASP.NET cache should " + 
      "ignore the Cache-Control headers sent by the client that invalidate the cache.");
}

</script>
</head>
<body></body>
</html>
<!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>
    <title>ASP.NET Example</title>
<script language="VB" runat="server">

' The following example demonstrates the SetValidUntilExpires method of the
' HttpCachePolicy class. The SetValidUntilExpires method is set to true so that
' the ASP.NET cache ignores the HTTP Cache-Control headers and the page remains 
' in the cache until it expires.

Sub Page_Load(Sender As Object, e As EventArgs)
   ' Set the expiration time for the page.
   Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
   ' Set the VaryByHeaders attribute with the value Accept-Language to true.
   Response.Cache.VaryByHeaders("Accept-Language") = True
   ' ASP.NET ignores cache invalidation headers and the page remains in
   ' the cache until it expires.
   Response.Cache.SetValidUntilExpires(True)
   Response.Write("The SetValidUntilExpires method is set to true and the ASP.NET cache will " _
      & " ignore the Cache-Control headers sent by the client that invalidate the cache.")
End Sub 'Page_Load

</script>
</head>
<body></body>
</html>

注解

使用高级<%@ OutputCache … %>页面指令时,该方法SetValidUntilExpires将自动设置为true

此方法之所以提供,是因为某些浏览器在刷新页面视图时,将 HTTP 缓存无效标头发送到 Web 服务器,并从缓存中逐出页面。 validUntilExpires参数完成后true,ASP.NET 将忽略缓存无效标头,页面将保留在缓存中,直到缓存过期。

SetValidUntilExpires在 .NET Framework 版本 3.5 中引入。 有关详细信息,请参见版本和依赖关系

适用于