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")
});
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Disabling browser cache in ASP.NET core:
public class HomeController : Controller
{
[ResponseCache(NoStore =true, Location =ResponseCacheLocation.None)]
public IActionResult Index()
{
return View();
}
}
Method 2
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/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a1acacb7b0b7b1a2b3eda0b0b0fc8387a2b7a697aaaea6ed8dacb4ed97aaa0a8b0">[email protected]</a>" rel="nofollow noreferrer noopener" />
<link rel="stylesheet" href="~/css/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a29332e3f74392929651a1e3b2e3f0e33373f7414352d740e33393129">[email protected]</a>" rel="nofollow noreferrer noopener" />
</environment>
Method 3
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.
Method 4
[ResponseCache(Location = ResponseCacheLocation.None, Duration = 0, NoStore = true)]
Try adding an annotation above the controller class.It works for me.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0