Asp.net 核心缓存 + IE 11 导致缓存响应

Asp.net Core Caching + IE 11 Results in Cached Responses

我在缓存方面遇到了困难,首先我遇到了 Google Chrome 缓存前端 reactjs 代码的问题,尽管我向 javascript 添加了一个散列并且css 文件(似乎正在缓存 index.html 文件??)。

现在 IE 11 似乎正在缓存我调用的 api。例如,我加载 IE 11 并发出请求,然后按 F5 并查看网络选项卡,我会看到请求将是 "Received From Cache" 即使我知道来自 [=] 的 100% 数据,它也会始终显示23=] 已更改。

其他浏览器没有这个问题。

您可以在控制器或方法上使用此属性来控制缓存策略:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class NoCacheAttribute : ResponseCacheAttribute
    {
        public NoCacheAttribute()
        {
            Duration = 0;
            NoStore = true;
            Location = ResponseCacheLocation.None;
        }
    }

然后在需要的地方使用这个属性:

    [HttpGet]
    [NoCache]
    public async Task<IActionResult> Get()
    {
        ... you code here
    }

您还可以向您的属性添加一些参数来微调您的缓存策略。

尝试使用以下代码在操作上添加缓存策略

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

或全球

services.AddMvc(options => {
    options.Filters.Add(new ResponseCacheAttribute() { NoStore = true, Location =  ResponseCacheLocation.None });
})