WebRequest.DefaultCachePolicy 屬性

定義

取得或設定這個要求的預設快取原則。

public:
 static property System::Net::Cache::RequestCachePolicy ^ DefaultCachePolicy { System::Net::Cache::RequestCachePolicy ^ get(); void set(System::Net::Cache::RequestCachePolicy ^ value); };
public static System.Net.Cache.RequestCachePolicy? DefaultCachePolicy { get; set; }
public static System.Net.Cache.RequestCachePolicy DefaultCachePolicy { get; set; }
member this.DefaultCachePolicy : System.Net.Cache.RequestCachePolicy with get, set
Public Shared Property DefaultCachePolicy As RequestCachePolicy

屬性值

RequestCachePolicy

HttpRequestCachePolicy,在沒有其他適用的原則時,為這個要求指定作用中的快取原則。

範例

下列程式碼範例示範如何設定 Web 要求的預設快取原則。

static WebResponse^ GetResponseFromServer2( Uri^ uri )
{
   RequestCachePolicy^ policy = gcnew RequestCachePolicy( RequestCacheLevel::NoCacheNoStore );
   WebRequest^ request = WebRequest::Create( uri );
   WebRequest::DefaultCachePolicy = policy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"Policy is {0}.", policy );
   Console::WriteLine( L"Is the response from the cache? {0}", response->IsFromCache );
   return response;
}
        public static WebResponse GetResponseFromServer2(Uri uri)
{
     RequestCachePolicy policy =
        new  RequestCachePolicy( RequestCacheLevel.NoCacheNoStore);
    WebRequest request = WebRequest.Create(uri);
    WebRequest.DefaultCachePolicy = policy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("Policy is {0}.", policy.ToString());
    Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);
    return response;
}

備註

如果存在下列條件,此原則會用於此要求:

  • 沒有 DefaultCachePolicy 為此要求指定的屬性。

  • 電腦和應用程式組態檔未指定適用于用來建立此要求的統一資源識別項 (URI) 快取原則。

快取原則會決定是否可以從快取取得要求的資源,而不是將要求傳送至資源主機電腦。

只有在擷取並讀取至資料流程的回應資料流程時,才會將資源的複本新增至快取。 因此,相同資源的另一個要求可以使用快取的複本,視此要求的快取原則層級而定。

適用於

另請參閱