How to enable ClientCache in ASP.NET core

In ASP.net 4.5, we used the ability to run static resource headers (in turn, enabling browser caching) by adding "ClientCache" to web.config, something like:

<staticcontent> <clientcache cachecontrolmode="UseMaxAge" cachecontrolmaxage="365.00:00:00" /> </staticcontent> 

As stated at http://madskristensen.net/post/cache-busting-in-aspnet

How to do it now in ASP.net 5 if we do not have web.config and Startup.cs files?

+6
source share
3 answers

In Startup.cs> Configure (IApplicationBuilder applicationBuilder, .....)

 applicationBuilder.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = context => context.Context.Response.Headers.Add("Cache-Control", "public, max-age=2592000") }); 
+8
source

If you use MVC, you can use ResponseCacheAttribute in your actions to set client cache headers. There is also a ResponseCacheFilter that you can use.

+1
source

Which server are you using?

  • If you use IIS, you can still use web.config in your wwwroot folder.

  • If you use kestrel, there is no built-in solution yet. However, you can write middleware that adds a specific cache control header. Or use nginx as a reverse proxy.

Middleware:

NOT tested (!) And only on my head can you write something like this:

 public sealed class CacheControlMiddleWare { readonly RequestDelegate _next; public CacheControlMiddleWare(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { if (context.Request.Path.Value.EndsWith(".jpg") { context.Response.Headers.Add("Cache-Control", new[]{"max-age=100"}); } await _next(context); } } 

nginx as a reverse proxy:

http://mjomaa.com/computer-science/frameworks/asp-net-mvc/141-how-to-combine-nginx-kestrel-for-production-part-i-installation

In addition, I wrote some notes for caching responses:

http://mjomaa.com/computer-science/frameworks/asp-net-mvc/153-output-response-caching-in-asp-net-5

+1
source

All Articles