.NET Library Documentation

System.Net.Http.Headers.CacheControlHeaderParser

Parses the Cache-Control header.

Class Members

Methods

Signature Description
static CacheControlHeaderValue Parse(string value) Parses a Cache-Control header string into a CacheControlHeaderValue object.
static bool TryParse(string value, out CacheControlHeaderValue parsedValue) Attempts to parse a Cache-Control header string into a CacheControlHeaderValue object. Returns true if parsing succeeds, false otherwise.

Related Classes

Remarks

The CacheControlHeaderParser class is used internally by the .NET framework to handle the parsing of the Cache-Control HTTP header. It converts the string representation of the header into a structured object that can be easily manipulated.

The Cache-Control header provides directives for caching mechanisms in both requests and responses. These directives specify how the resource should be cached and for how long.

Example Usage

While you typically won't directly use the CacheControlHeaderParser, you'll interact with its output via the CacheControlHeaderValue class.

using System.Net.Http;
using System.Net.Http.Headers;

// Assume response is an HttpResponseMessage
HttpResponseMessage response = /* ... get your response ... */;

if (response.Headers.CacheControl != null)
{
    CacheControlHeaderValue cacheControl = response.Headers.CacheControl;

    Console.WriteLine($"No-Cache: {cacheControl.NoCache}");
    Console.WriteLine($"No-Store: {cacheControl.NoStore}");
    Console.WriteLine($"Max-Age: {cacheControl.MaxAge}"); // TimeSpan

    foreach (var directive in cacheControl.Extensions)
    {
        Console.WriteLine($"Extension: Name='{directive.Name}', Value='{directive.Value}'");
    }
}
else
{
    Console.WriteLine("No Cache-Control header found.");
}

See Also