HTTP-based caching
To control the browsers cache you can set a Cache-Control
HTTP header. Usually, the StaticFileMiddleware
doesn't set a Cache-Control header. This means the clients are free to cache how they prefer. If you like to limit the cache time to just one day, you can do this by passing StaticFileOptions
to the middleware:
const string cacheMaxAge = "86400"; app.UseStaticFiles(new StaticFileOptions() { OnPrepareResponse = ctx => { ctx.Context.Response.Headers.TryAdd( "Cache-Control", $"public, max-age={cacheMaxAge}"); } });
This sets the Cache-Control
header to every static file that is requested before it gets sent to the client. The Cache-Control
is set to public, which...