ASP.NET核心响应缓存无法按预期工作

问题描述 投票:2回答:1

我很难在ASP.NET Core 2.0 Web API中获得响应缓存。

我在中间件中添加了响应缓存:

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

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

这是我的动作方法代码:

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

这是邮递员的请求和回复:enter image description here

所以,我在响应中获得了正确的Cache-Control http标头,但是如果我再次从邮递员发送响应,它仍然在我的action方法中调用我的代码。我希望它不会调用我的代码并使用缓存的响应。

我是否误解了响应缓存的工作方式?任何帮助,将不胜感激。

http asp.net-core postman
1个回答
3
投票

Postman的问题是你必须禁用Send-No-Cash标头(无缓存标头确保你从服务器获得最新的响应)

ASP.NET Core 2.0 Web API Response Caching

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.