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; }
static member DefaultCachePolicy : System.Net.Cache.RequestCachePolicy with get, set
Public Shared Property DefaultCachePolicy As 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) 的缓存策略。

缓存策略确定是否可以从缓存获取请求的资源,而不是将请求发送到资源主计算机。

仅当检索资源响应流并将其读取到流的末尾时,才会将资源的副本添加到缓存中。 因此,针对同一资源的另一个请求可以使用缓存副本,具体取决于此请求的缓存策略级别。

适用于

另请参阅