Class CacheControl
Common CacheControlHeaderValue presets for use with WithCacheControl(CacheControlHeaderValue) and WithCacheControl(CacheControlHeaderValue).
public static class CacheControl
- Inheritance
-
CacheControl
- Inherited Members
Remarks
Every helper returns a fresh CacheControlHeaderValue on each call. CacheControlHeaderValue is mutable, so mutating an instance returned by one of these helpers does not leak into a later call.
Use directly: opts.WithCacheControl(CacheControl.NoStore()), or pick the BCL type
directly when you need a directive that isn't represented as a property on
CacheControlHeaderValue.
Methods
Immutable(TimeSpan)
Cache-Control: public, max-age={seconds}, immutable — the response will not change for
its freshness lifetime, so clients should not revalidate. The immutable directive
(RFC 8246) is appended via the Extensions collection
because the BCL type has no dedicated property for it.
public static CacheControlHeaderValue Immutable(TimeSpan maxAge)
Parameters
maxAgeTimeSpanFreshness lifetime; emitted as
max-agein seconds. Must be non-negative.
Returns
Exceptions
- ArgumentOutOfRangeException
maxAgeis negative.
NoCache()
Cache-Control: no-cache — caches must revalidate with the origin before serving.
public static CacheControlHeaderValue NoCache()
Returns
Remarks
Different from no-store: no-cache allows storage but requires revalidation.
NoStore()
Cache-Control: no-store — the response must not be stored anywhere.
public static CacheControlHeaderValue NoStore()
Returns
Remarks
Use for responses that may contain personal data, secrets, or per-user state. Applies to both success and failure responses through WithCacheControl(CacheControlHeaderValue) — a 404 from a sensitive endpoint is no less sensitive than the 200.
Private(TimeSpan)
Cache-Control: private, max-age={seconds} — only the requesting user agent may store the response.
public static CacheControlHeaderValue Private(TimeSpan maxAge)
Parameters
maxAgeTimeSpanFreshness lifetime; emitted as
max-agein seconds (truncated to whole seconds). Must be non-negative — RFC 9111delta-secondsis unsigned.
Returns
Exceptions
- ArgumentOutOfRangeException
maxAgeis negative.
Public(TimeSpan)
Cache-Control: public, max-age={seconds} — shared caches may store the response.
public static CacheControlHeaderValue Public(TimeSpan maxAge)
Parameters
maxAgeTimeSpanFreshness lifetime; emitted as
max-agein seconds (truncated to whole seconds). Must be non-negative — RFC 9111delta-secondsis unsigned.
Returns
Exceptions
- ArgumentOutOfRangeException
maxAgeis negative.