36

I tried this Middleware but the browser still saving files.

I want user will always get the last version of js and css files.

public void Configure(IApplicationBuilder app)
{
    app.UseSession();
    app.UseDefaultFiles();
    app.UseStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = context =>
            context.Context.Response.Headers.Add("Cache-Control", "no-cache")
    });
}
Mr Rogers
  • 6,091
  • 2
  • 32
  • 34
David Munsa
  • 885
  • 1
  • 16
  • 30

5 Answers5

45

Try adding an Expires header as well:

app.UseStaticFiles(new StaticFileOptions()
{
    OnPrepareResponse = context =>
    {
        context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
        context.Context.Response.Headers.Add("Expires", "-1");
    }
});

Another approach would be to add a querystring that changes to the end of your requests in development. Middleware would not be required in this case.

<environment names="Development">
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css?@DateTime.Now.Ticks" />
    <link rel="stylesheet" href="~/css/site.css?@DateTime.Now.Ticks" />
</environment>
Will Ray
  • 10,621
  • 3
  • 46
  • 61
  • 10
    If you do Headers.Add(, then it will fail if a middleware already added the header, therefore, use Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate, max-age=0"; etc. – Stefan Steiger Sep 05 '19 at 13:05
44

Disabling browser cache in ASP.NET core:

public class HomeController : Controller
{
    [ResponseCache(NoStore =true, Location =ResponseCacheLocation.None)]
    public IActionResult Index()
    {
        return View();
    }
}
Mentor
  • 3,058
  • 1
  • 22
  • 27
15

Another way would be using an ASP-Attribute when you link your files in your _Layout.cshtml by using asp-append-version you will add a fresh hash everytime the file changed, so writing:

<script src="~/js/minime.js" asp-append-version="true"></script>

will in the end lead to:

<script src="/js/minime.js?v=Ynfdc1vuMOWZFfqTjfN34c2azo3XiIfgfE-bba1"></script>

so you get caching and the latest version out of the box.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
MushyPeas
  • 2,469
  • 2
  • 31
  • 48
  • The hash is defined in LinkTagHelper, currently a SHA256-hash (SHA: Secure Hash Algorithms): https://stackoverflow.com/questions/33342643/how-does-javascript-version-asp-append-version-work-in-asp-net-core-mvc – Stefan Steiger Sep 05 '19 at 12:37
3
[ResponseCache(Location = ResponseCacheLocation.None, Duration = 0, NoStore = true)]

Try adding an annotation above the controller class.It works for me.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
0

I had the same issue, but the posted solution didn't work for me. What was working is that I added a middleware, which adds the headers.

    app.Use(async (httpContext, next) =>
    {
        httpContext.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate, max-age=0";
        httpContext.Response.Headers[HeaderNames.Expires] = "-1";
        await next();
    });
KnApP993
  • 66
  • 2
  • 9