The no-store directive tells caches not to store a response. It is useful when a response contains request-specific or sensitive information, such as error details, passwords, payment data, or personal data. section 3.1.3
In an ASP.NET Core web API, apply no-store with the [ResponseCache(NoStore = true)] attribute on an endpoint. This is clearer and easier to maintain than writing the Cache-Control header manually. section 3.1.3 section 8.2.1
Use the directive in these practical situations:
- Disable caching for error responses whose contents are unique to a particular request. section 3.1.3
- Add a fallback middleware policy when an endpoint does not explicitly specify a caching strategy. section 8.2.2
- Prefer an explicit public cache with a short max-age for reusable data instead of applying no-store everywhere. section 3.1.3
Protecting API error responses
Mark Minimal API error endpoints with [ResponseCache(NoStore = true)]. For example, an /error endpoint returning Results.Problem() and an /error/test endpoint throwing an exception both prevent response caching.
