Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable ClientCache in ASP.NET Core

In ASP.net 4.5 we used to be able to enable expires headers on static resources (in-turn, enabling browser caching) by adding 'ClientCache' to the web.config, something like:

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

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

How do we do this now in ASP.net 5 when we have no web.config and Startup.cs?

like image 889
Martin Kearn Avatar asked Aug 21 '15 10:08

Martin Kearn


2 Answers

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

applicationBuilder.UseStaticFiles(new StaticFileOptions
{
     OnPrepareResponse = context => 
     context.Context.Response.Headers.Add("Cache-Control", "public, max-age=2592000")
});
like image 198
Thomas Kold Avatar answered Sep 30 '22 01:09

Thomas Kold


If you are using MVC you can use the ResponseCacheAttribute on your actions to set client cache headers. There is also a ResponseCacheFilter you can use.

like image 37
Andreas Avatar answered Sep 30 '22 01:09

Andreas