NoCacheMiddleware Class

Ensures responses are marked as non-cacheable when no Cache-Control header is already set.

public sealed class NoCacheMiddleware

Remarks

Register this middleware after any component that serves cacheable content, such as UseStaticFiles: the static file middleware sets ETag and Last-Modified but no Cache-Control, so an earlier registration marks every static asset as non-cacheable.

Because the default includes no-store, responses defaulted by this middleware are not stored by UseResponseCaching, UseOutputCache or a CDN. Responses that set their own Cache-Control are never modified.

Constructors

NameDescription
NoCacheMiddleware(RequestDelegate)Ensures responses are marked as non-cacheable when no Cache-Control header is already set.

NoCacheMiddleware(RequestDelegate)

NoCacheMiddleware.cs

Ensures responses are marked as non-cacheable when no Cache-Control header is already set.

public NoCacheMiddleware(RequestDelegate next)

Parameters

next RequestDelegate
The next middleware in the pipeline.

Remarks

Register this middleware after any component that serves cacheable content, such as UseStaticFiles: the static file middleware sets ETag and Last-Modified but no Cache-Control, so an earlier registration marks every static asset as non-cacheable.

Because the default includes no-store, responses defaulted by this middleware are not stored by UseResponseCaching, UseOutputCache or a CDN. Responses that set their own Cache-Control are never modified.

Methods

NameDescription
InvokeAsync(HttpContext)Processes the request and adds a default non-cacheable Cache-Control header to the response when none is set.

InvokeAsync(HttpContext)

NoCacheMiddleware.cs

Processes the request and adds a default non-cacheable Cache-Control header to the response when none is set.

net10.0

public Task InvokeAsync(HttpContext context)

net11.0

public Task InvokeAsync(HttpContext context)

Parameters

context HttpContext
The current HTTP context.

Returns

Task A task that completes when the middleware has finished processing.