RequestCacheLevel Enum
Overview
The RequestCacheLevel
enumeration specifies the caching behavior for a request made by the System.Net.WebRequest
class and its derivatives.
Namespace: System.Net
Assembly: System.dll
Members
Name | Value | Description |
---|---|---|
BypassCache | 0 | Never use the cache. |
CacheOnly | 1 | Use only the cache; fail if the item is not cached. |
CacheIfAvailable | 2 | Use the cache if the item exists, otherwise retrieve it from the network. |
Revalidate | 3 | Validate the cached entry with the server before using it. |
Default | 4 | Determine the caching behavior based on the request's URI scheme. |
NoCacheNoStore | 5 | Do not cache and do not store the response locally. |
Refresh | 6 | Retrieve a fresh copy from the server, updating the cache. |
CacheOrNextRequest | 7 | Use the cached item if present; otherwise, store the next response in the cache. |
Examples
Below is a simple console application that demonstrates how to set the RequestCacheLevel
for a WebRequest
object.
using System;
using System.Net;
class Program
{
static void Main()
{
var url = "https://example.com/api/data";
var request = WebRequest.Create(url);
request.CachePolicy = new HttpRequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
using var response = request.GetResponse();
Console.WriteLine($"Response received from {(response.ResponseUri)}");
}
}