編輯

共用方式為


HttpCacheVaryByContentEncodings Class

Definition

Provides a type-safe way to set the VaryByContentEncodings property of the HttpCachePolicy class.

public ref class HttpCacheVaryByContentEncodings sealed
public sealed class HttpCacheVaryByContentEncodings
type HttpCacheVaryByContentEncodings = class
Public NotInheritable Class HttpCacheVaryByContentEncodings
Inheritance
HttpCacheVaryByContentEncodings

Examples

The following example shows how to enable a dynamically compressed response that can 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 hosts the page does not have dynamic compression enabled, the output cache will not have a cached response for 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 is used to specify whether dynamically compressed responses are cached. Caching dynamically compressed responses means that the cost of compression is incurred only one time, during the first request for the resource (or after an application restart) and when the cache item expires.

The VaryByContentEncodings property of the HttpCachePolicy class identifies which request header parameters ASP.NET uses to uniquely identify a variation of the response if there are multiple cached responses for a resource. This is useful when a response depends on a set of client inputs, such as headers, parameters, or content encodings.

The HttpCacheVaryByContentEncodings class is not directly related to HTTP cache-control headers. However, it helps make sure that a response to a client or a proxy varies by the specified content encoding. Setting the cache to vary by content encoding enables ASP.NET to vary the response by 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 also found in the VaryByContentEncodings list and a cached response exists, the cached response is sent.

  • If a matching encoding is also 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 identity response is found, it is sent. Otherwise a new non-encoded response is generated and stored in the cache.

For more information about the VaryByContentEncodings property, see RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1, available on the World Wide Web Consortium (W3C) Web site. See section 14, "Header Field Definitions", for complete details.

You can set the VaryByContentEncodings property by using the @ OutputCache directive or by adding a profile to the outputCacheProfile element in the Web.config file.

Constructors

HttpCacheVaryByContentEncodings()

Initializes a new instance of the HttpCacheVaryByContentEncodings class.

Properties

Item[String]

Gets or sets a value that indicates whether the cache varies according to the specified content encoding.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetContentEncodings()

Gets an array of values which specify the content encodings to be used for varying the cache.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SetContentEncodings(String[])

Sets an array of values which specify the content encodings to be used for varying the cache.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also