Table of Contents

Class CacheControl

Namespace
Trellis.Asp
Assembly
Trellis.Asp.dll
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

maxAge TimeSpan

Freshness lifetime; emitted as max-age in seconds. Must be non-negative.

Returns

CacheControlHeaderValue

Exceptions

ArgumentOutOfRangeException

maxAge is negative.

NoCache()

Cache-Control: no-cache — caches must revalidate with the origin before serving.

public static CacheControlHeaderValue NoCache()

Returns

CacheControlHeaderValue

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

CacheControlHeaderValue

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

maxAge TimeSpan

Freshness lifetime; emitted as max-age in seconds (truncated to whole seconds). Must be non-negative — RFC 9111 delta-seconds is unsigned.

Returns

CacheControlHeaderValue

Exceptions

ArgumentOutOfRangeException

maxAge is negative.

Public(TimeSpan)

Cache-Control: public, max-age={seconds} — shared caches may store the response.

public static CacheControlHeaderValue Public(TimeSpan maxAge)

Parameters

maxAge TimeSpan

Freshness lifetime; emitted as max-age in seconds (truncated to whole seconds). Must be non-negative — RFC 9111 delta-seconds is unsigned.

Returns

CacheControlHeaderValue

Exceptions

ArgumentOutOfRangeException

maxAge is negative.