5
votes

I'm struggling to get response caching in an ASP.NET Core 2.0 web API working.

I've added response caching in the middleware:

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCaching();
    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCaching();
    ...
}

Here my action method code:

[ResponseCache(Duration = 30)]
[HttpGet()]
public IActionResult GetLookups()
{
    Lookup lookups = dataRepository.GetContactLookups();
}

Here's the postman request and response: enter image description here

So, I'm getting the correct Cache-Control http header in the response but if I send the response again from postman, it still calls my code in my action method. I expected it to not call my code and use the cached response.

Am I misunderstanding the way that response caching works? Any help would be appreciated.

1

1 Answers

7
votes

The problem with Postman you have to disable Send-No-Cash header ( no-cache header makes sure you get the freshest response from your server)

ASP.NET Core 2.0 Web API Response Caching

enter image description here