lemunk
lemunk

Reputation: 2636

.Net CORE Web API no-cache but still happens

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.

Upvotes: 3

Views: 6883

Answers (1)

Tinwor
Tinwor

Reputation: 7983

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.

Upvotes: 8

Related Questions