.Net CORE Web API无缓存但仍然会发生

lem*_*unk 3 c# asp.net-web-api asp.net-core

使用.Net Core和visual studio 2015.

我在.net核心中创建了一个Web API,最近在测试中我意识到我的结果正在被缓存(或者至少看起来像是).所以我建立了一个响应缓存并将位置设置为none:

[Route("api/[controller]")]
public class NopProductController : Controller
{
    private INopProductService _nopProductService { get; set; }
    public NopProductController(INopProductService nopProductService)
    {
        _nopProductService = nopProductService;
    }

    [HttpGet]
    [ResponseCache(Duration = 60, Location = ResponseCacheLocation.None)]
    public IActionResult GetChangedProducts()
    {
        return Ok(_nopProductService.GetChanged());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我检查邮差并检查标题时,我发现:

Cache-Control→no-cache,max-age = 60 Pragma→no-cache

这看起来是正确的我相信.但是,当我在我的Sql Server 2012数据库中编辑相关表上的数据(我更改1单元格值)然后刷新请求时,更改不存在.

如您所见,看起来缓存已关闭.那么为什么不会发生变化,只有当我重置IIS时才会发生变化.

这是Kestrel和IIS的问题吗?有什么错误或我在这里错过的东西?

注意:WebAPI是在本地发布的,用于测试+在Windows 10上通过IIS托管.

Tin*_*wor 5

要请求缓存,您应该NoStore = trueResponseCache属性类中设置并删除该Duration属性.
设置此属性后,您将获得此标题:

Cache-Control: no-store,no-cache
Pragma: no-cache
Run Code Online (Sandbox Code Playgroud)

正如评论中所提到的,它是一个与entityframework 6相关的缓存问题,为了避免缓存,你应该AsNoTracking()在转换类型方法之前使用,如果你有一个当然.