.Net CORE Web API没有缓存,但仍然会发生 [英] .Net CORE Web API no-cache but still happens

查看:60
本文介绍了.Net CORE Web API没有缓存,但仍然会发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.Net Core和Visual Studio 2015.

Using .Net Core and visual studio 2015.

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

I have a Web API create in .net core, Recently in testing i realized my results are being cached (or atleast appear to be). So i implimented a response cache and set location to 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());
    }
}

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

When i checked Postman and checked the headers, i found:

缓存控制→不缓存,最大年龄= 60实用→无缓存

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

我相信这看起来是正确的.但是,当我在有问题的表上的Sql Server 2012 DB中编辑数据时(我更改了1个单元格的值),然后刷新请求,更改就没有了.

which looks correct i beleive. However when i edit data in my Sql Server 2012 DB on the table in question (i change 1 cell value) then refresh the request, the change isnt there.

如您所见,似乎缓存已关闭.那么为什么不发生更改,只有当我重置IIS时更改才会通过.

As you can see it appears caching is turned off. So why would no change happen, only when i reset IIS does the change come through.

这是Kestrel和IIS的问题吗?这里有一些错误或我想念的东西吗?

Is this an issue with Kestrel and IIS? is there some bug or something i missed here?

注意:WebAPI在Windows 10上通过IIS在本地发布以进行测试+托管.

Note: WebAPI is published locally for testing + hosted via IIS on windows 10.

推荐答案

要请求缓存,您应该在 ResponseCache 属性类中设置 NoStore = true 并删除持续时间属性.
设置此属性后,您将获得以下标头:

To request caching you should set NoStore = true inside the ResponseCache attribute class and remove the Duration attribute.
After you have set this attribute you will get this Headers:

Cache-Control: no-store,no-cache
Pragma: no-cache

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

As mentioned in the comments it is a caching problem related to entityframework 6 and to avoid caching you should use AsNoTracking() just before the conversion type method, if you have one of course.

这篇关于.Net CORE Web API没有缓存,但仍然会发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆