HttpCacheVaryByContentEncodings 類別

定義

提供類型安全方式來設定 HttpCachePolicy 類別的 VaryByContentEncodings 屬性。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
繼承
HttpCacheVaryByContentEncodings

範例

下列範例顯示如何啟用可從輸出快取提供的動態壓縮回應。The following example shows how to enable a dynamically compressed response that can be served from the output cache. 可接受的編碼為 "gzip",且使用指示詞的 VaryByContentEncodings 屬性設定。The encoding that is acceptable is "gzip" and is set by using the VaryByContentEncodings attribute of the 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>

備註

VaryByContentEncodings屬性可用來指定是否要快取動態壓縮的回應。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.

VaryByContentEncodings如果資源有多個快取回應,則類別的屬性 HttpCachePolicy 會識別 ASP.NET 用來唯一識別回應變化的要求標頭參數。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.

類別與 HTTP 快取 HttpCacheVaryByContentEncodings 控制標頭不直接相關。The HttpCacheVaryByContentEncodings class is not directly related to HTTP cache-control headers. 不過,它有助於確保用戶端或 proxy 的回應會因指定的內容編碼而異。However, it helps make sure that a response to a client or a proxy varies by the specified content encoding. 將快取設定為隨內容編碼而改變,可讓 ASP.NET 依 Accept-Encoding 標頭改變回應。Setting the cache to vary by content encoding enables ASP.NET to vary the response by the Accept-Encoding header. 處理要求時, Accept-Encoding 會檢查標頭,並識別第一個可接受的編碼,並使用下列其中一個動作: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:

  • 如果清單中也有相符的編碼方式,而且存在快取的 VaryByContentEncodings 回應,就會傳送快取的回應。If a matching encoding is also found in the VaryByContentEncodings list and a cached response exists, the cached response is sent.

  • 如果在清單中找到相符的編碼,但快取的 VaryByContentEncodings 回應不存在,就會產生回應並將其插入快取中。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.

  • 如果在清單中找不到相符的編碼,則會在快取中 VaryByContentEncodings 搜尋非編碼的回應,也稱為身分識別回應。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.

如需屬性的詳細資訊 VaryByContentEncodings ,請參閱 全球資訊網協會 (W3C) 網站上提供的 RFC 2616:超文字傳輸通訊協定--HTTP/1.1。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. 如需完整的詳細資訊,請參閱第14節:「標頭欄位定義」。See section 14, "Header Field Definitions", for complete details.

您可以使用指示詞 VaryByContentEncodings 或將設定檔新增至 Web.config 檔案中的專案,來設定屬性。You can set the VaryByContentEncodings property by using the directive or by adding a profile to the element in the Web.config file.

建構函式

HttpCacheVaryByContentEncodings()

初始化 HttpCacheVaryByContentEncodings 類別的新執行個體。Initializes a new instance of the HttpCacheVaryByContentEncodings class.

屬性

Item[String]

取得或設定值,這個值表示快取是否會依據指定的內容編碼方式而有所不同。Gets or sets a value that indicates whether the cache varies according to the specified content encoding.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 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.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 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.

(繼承來源 Object)

適用於

另請參閱