Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add no-cache to Kestrel responses?

I'm using Asp.Net Core RC2 and Kestrel as my web server. I need to ensure that requests (in this case all of them) are responded to with a no-cache header so that the browsers get the newest version (not 304).

Is there a way in Startup to configure Kestrel or a way to inject this step into the pipeline?

EDIT: no-store may be a better choice in my situation: https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching "no-store Response is not allowed to be cached and must be fetched in full on every request."

like image 318
AlignedDev Avatar asked May 02 '16 16:05

AlignedDev


People also ask

What does no cache cache directive do?

no-cache. The no-cache response directive indicates that the response can be stored in caches, but the response must be validated with the origin server before each reuse, even when the cache is disconnected from the origin server.

What should you add to a cache-control response header to specify that a response should not be stored in an intermediary cache?

The cache-control "no-store" option will prevent the request and response from being stored by the cache. Adding the "private" option will prevent proxies from caching the page.

How do you use response cache?

To enable caching, Duration must be set to a positive value and Location must be either Any (the default) or Client . The framework sets the Cache-Control header to the location value followed by the max-age of the response.

What is no cache No store?

Cache-Control: No-Store The no-store directive means browsers aren't allowed to cache a response and must pull it from the server each time it's requested. This setting is usually used for sensitive data, such as personal banking details.


1 Answers

You can use middleware to work with headers. For example, you can force no-cache cache-control by adding the following to the top of your Startup's Configure method:

app.Use(async (httpContext, next) =>
{
    httpContext.Response.Headers[HeaderNames.CacheControl] = "no-cache";
    await next();
});
like image 166
N. Taylor Mullen Avatar answered Sep 25 '22 01:09

N. Taylor Mullen