HttpCachePolicy.SetValidUntilExpires Method (Boolean)

 

Specifies whether the ASP.NET cache should ignore HTTP Cache-Control headers sent by the client that invalidate the cache.

Namespace:   System.Web
Assembly:  System.Web (in System.Web.dll)

public void SetValidUntilExpires(
	bool validUntilExpires
)

Parameters

validUntilExpires
Type: System.Boolean

true if the cache ignores Cache-Control invalidation headers; otherwise, false.

The SetValidUntilExpires method is set to true automatically when the high-level <%@ OutputCache … %> page directive is used.

This method is provided because some browsers, when refreshing a page view, send HTTP cache invalidation headers to the Web server and evict the page from the cache. When the validUntilExpires parameter is true, ASP.NET ignores cache invalidation headers and the page remains in the cache until it expires.

SetValidUntilExpires is introduced in the .NET Framework version 3.5. For more information, see .NET Framework Versions and Dependencies.

The following code example demonstrates how to use the SetValidUntilExpires method to instruct that any cache invalidation headers sent by the client are ignored.

<!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>

.NET Framework
Available since 1.1
Return to top
Show: