asp.net-mvcweb-configasp.net-coreclientcache

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?


Solution

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

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