HttpCachePolicy.VaryByContentEncodings 屬性

定義

取得將用於變更輸出快取的 Content-Encoding 標頭清單。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

屬性值

HttpCacheVaryByContentEncodings

指定用來選取快取回應之 Content-Encoding 標頭的物件。An object that specifies which Content-Encoding headers are used to select the cached response.

範例

下列範例示範如何啟用從輸出快取提供的頁面動態壓縮回應。The following example shows how to enable a dynamically compressed response for a page to be served from the output cache. 可接受的編碼為 "gzip",且是使用 VaryByContentEncodings @ OutputCache 指示詞的屬性來設定。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>

備註

VaryByContentEncodings屬性會傳回內容編碼標頭的清單,這些標頭表示用來變更輸出快取的壓縮配置。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.

VaryByContentEncodings 屬性會與要求的 Accept-Encoding 標頭搭配使用,以決定如何為動態壓縮的不同內容編碼提供快取回應。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. Accept-Encoding 標頭會列出用戶端可以解壓縮的編碼方式。The Accept-Encoding header lists the encodings that the client can decompress.

當您將快取設定為隨內容編碼而改變時,它會讓回應以間接方式由標頭中的值來變更 Accept-EncodingWhen you set the cache to vary by content encoding, it lets the response vary indirectly by the value in 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 found in the VaryByContentEncodings list and a cached response exists, the cached response is sent.

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

  • 如果在清單中找不到相符的編碼,則會在快取中 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 non-encoded response is found, it is sent. 否則,會產生並傳送新的非編碼回應,並將其儲存在快取中。Otherwise, a new non-encoded response is generated, sent, 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," which is available on the World Wide Web Consortium (W3C) Web site. 內容編碼的相關資訊位於第14節「標頭欄位定義」。Information about content encodings is in section 14, "Header Field Definitions."

您可以 VaryByContentEncodings 使用 VaryByContentEncodings @ OutputCache 指示詞的屬性來設定屬性。You can set the VaryByContentEncodings property by using the VaryByContentEncodings attribute of the @ OutputCache directive. 您也可以將快取設定檔新增至 Web.config 檔案中的 outputCacheProfile 元素。You can also add a cache profile to the outputCacheProfile element in the Web.config file.

VaryByContentEncodings 在 .NET Framework 版本3.5 中引進。VaryByContentEncodings is introduced in the .NET Framework version 3.5. 如需詳細資訊,請參閱版本和相依性For more information, see Versions and Dependencies.

適用於

另請參閱