title: How to cache static files in ASP.NET Core?
body: <p>I can't seem to enable caching of static files in ASP.NET Core 2.2. I have the following in my <code>Configure</code>:</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    if (env.IsDevelopment()) {
      app.UseDeveloperExceptionPage();
      app.UseCors(...);
    }
    else {
      app.UseHsts();
    }
  
    app.UseHttpsRedirection();
    app.UseAuthentication();
    app.UseSignalR(routes =&gt; { routes.MapHub&lt;NotifyHub&gt;("/..."); });
  
    app.UseResponseCompression();
    app.UseStaticFiles();
    app.UseSpaStaticFiles(new StaticFileOptions() {
      OnPrepareResponse = (ctx) =&gt; {
        ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public, max-age=31557600"; // cache for 1 year
      }
    });
    app.UseMvc();
  
    app.UseSpa(spa =&gt; {
      spa.Options.SourcePath = "ClientApp";
      if (env.IsDevelopment()) {
        spa.UseVueCli(npmScript: "serve", port: 8080);
      }
    });
  }
  </code></pre>
  
  <p>When I try and Audit the production site on HTTPS using chrome I keep getting "Serve static assets with an efficient cache policy":</p>
  
  <p><a href="https://i.stack.imgur.com/qNUlT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qNUlT.png" alt="audit screenshot"></a></p>
  
  <p>In the network tab there is no mention of caching in the headers, when I press F5 it seems everything is served from disk cache. But, how can I be sure my caching setting is working if the audit is showing its not?</p>
  
  <p><a href="https://i.stack.imgur.com/ewI4D.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ewI4D.png" alt="network tab screenshot"></a></p>