.Net CORE Web API no-cache but still happens

血红的双手。 提交于 2021-02-07 20:17:15

问题


Using .Net Core and visual studio 2015.

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:

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

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.

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.

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

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/41692208/net-core-web-api-no-cache-but-still-happens

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