Caching using ResponseCachingMiddleware
The ResponseCachingMiddleware
caches the responses on the server side and creates responses based on the cached responses. The middleware respects the Cache-Control
header in the same way as clients do. That means you are able to control the middleware by setting the specific headers as described in the previews section.
To get it working you need to add the ResponseCachingMiddleware
to the Dependency Injection container:
builder.Services.AddResponseCaching();
And you should use that middleware to the pipeline after the static files and routing got added:
app.UseResponseCaching();
If you added a CORS configuration, the UseCors
method should be called before, as well.
The ResponseCachingMiddleware
gets affected by specific HTTP headers. For example, if the Authentication
header is set the response doesn't get cached, same with the Set-Cookie
header. It also only caches responses that result in a 200 OK result. Error pages...