How to disable browser cache in ASP.NET core rc2?

此生再无相见时 提交于 2020-01-09 07:31:11

问题


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")
    });
}

回答1:


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>



回答2:


Disabling browser cache in ASP.NET core:

public class HomeController : Controller
{
    [ResponseCache(NoStore =true, Location =ResponseCacheLocation.None)]
    public IActionResult Index()
    {
        return View();
    }
}



回答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.



来源:https://stackoverflow.com/questions/38231739/how-to-disable-browser-cache-in-asp-net-core-rc2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!