HttpCachePolicy.VaryByContentEncodings Property

Definition

Gets the list of Content-Encoding headers that will be used to vary the output cache.

public:
 property System::Web::HttpCacheVaryByContentEncodings ^ VaryByContentEncodings { System::Web::HttpCacheVaryByContentEncodings ^ get(); };
public System.Web.HttpCacheVaryByContentEncodings VaryByContentEncodings { get; }
member this.VaryByContentEncodings : System.Web.HttpCacheVaryByContentEncodings
Public ReadOnly Property VaryByContentEncodings As HttpCacheVaryByContentEncodings

Property Value

An object that specifies which Content-Encoding headers are used to select the cached response.

Examples

The following example shows how to enable a dynamically compressed response for a page to be served from the output cache. The encoding that is acceptable is "gzip" and is set by using the VaryByContentEncodings attribute of the @ OutputCache directive. If the Web server that is hosting the page does not have dynamic compression enabled, the output cache will not have a cached response that is based on the specified content encoding.

<%@ Page Language="C#" %>
<%@ OutputCache VaryByParam="none" Duration="10" VaryByContentEncodings="gzip" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Varying Output Cache By Content Encoding</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%= DateTime.Now.ToString() %>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ OutputCache VaryByParam="none" Duration="10" VaryByContentEncodings="gzip" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Varying Output Cache By Content Encoding</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%= DateTime.Now.ToString() %>
    </div>
    </form>
</body>
</html>

Remarks

The VaryByContentEncodings property returns a list of Content-Encoding headers that represent compression schemes to vary the output cache by. Caching dynamically compressed responses means that the cost of compression is incurred only one time, during the first request for the resource.

The VaryByContentEncodings property is used with the Accept-Encoding header of a request to determine how to serve cached responses for different content encodings that are dynamically compressed. The Accept-Encoding header lists the encodings that the client can decompress.

When you set the cache to vary by content encoding, it lets the response vary indirectly by the value in the Accept-Encoding header. When a request is processed, the Accept-Encoding header is checked and the first acceptable encoding is identified and used to take one of the following actions:

  • If a matching encoding is found in the VaryByContentEncodings list and a cached response exists, the cached response is sent.

  • If a matching encoding is found in the VaryByContentEncodings list but a cached response does not exist, a response is generated and inserted into the cache.

  • If a matching encoding is not found in the VaryByContentEncodings list, the cache is searched for a non-encoded response, also referred to as the identity response. If the non-encoded response is found, it is sent. Otherwise, a new non-encoded response is generated, sent, and stored in the cache.

For more information about the VaryByContentEncodings property, see "RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1," which is available on the World Wide Web Consortium (W3C) Web site. Information about content encodings is in section 14, "Header Field Definitions."

You can set the VaryByContentEncodings property by using the VaryByContentEncodings attribute of the @ OutputCache directive. You can also add a cache profile to the outputCacheProfile element in the Web.config file.

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

Applies to

See also