-
在頁(yè)中包含 @ OutputCache 指令,并定義 Duration 和 VaryByParam 屬性。
-
在 @ OutputCache 指令中包含 Location 屬性,并將其值定義為 OutputCacheLocation
枚舉中的下列值之一:Any、Client、Downstream、Server、ServerAndClient 或 None。下面的代碼演示如何將頁(yè)的可緩存性設置為 60 秒:
<%@ OutputCache Duration="60" VaryByParam="None"%>
注意
默認設置為 Any。如果未定義 Location 屬性,則可以將頁(yè)輸出緩存在與響應有關(guān)的所有具有緩存功能的網(wǎng)絡(luò )設備上。其中包括請求客戶(hù)端、原服務(wù)器、以及響應通過(guò)的任何代理服務(wù)器。
使用緩存配置文件以聲明方式設置頁(yè)的可緩存性
-
在應用程序的 Web.config 文件中定義緩存配置文件,在配置文件中包括 duration 和
varyByParam 設置。下面的 <caching> 配置元素定義名為
Cache30Seconds的緩存配置文件,它將在服務(wù)器
上將頁(yè)緩存 30 秒之久。<caching> <outputCacheSettings> <outputCacheProfiles> <add name="Cache30Seconds" duration="30" varyByParam="none" /> </outputCacheProfiles> </outputCacheSettings> </caching>
-
在使用配置文件的每個(gè) ASP.NET 頁(yè)中包含 @ OutputCache 指令,并將 CacheProfile
屬性設置為 Web.config 文件中定義的緩存配置文件的名稱(chēng)。下面的代碼指定頁(yè)應當使用名為
Cache30Seconds的緩存配置文件:<%@ OutputCache CacheProfile="Cache30Seconds" %>



