Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Response Caching Not Working As Expected

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.

like image 646
Carl Rippon Avatar asked Dec 23 '17 22:12

Carl Rippon


1 Answers

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

like image 73
Ahmed Al Jabry Avatar answered Nov 15 '22 00:11

Ahmed Al Jabry