ASP.NET Core 2.0 Web API响应缓存

吐司先生

我正在尝试使用“ Microsoft.AspNetCore.ResponseCaching”包在我的Web api中实现响应缓存。

我正在使用Postman测试应用程序并验证标头等。

正确的标头是使用指定的最大年龄生成的,但是邮递员似乎从api请求响应,而不使用缓存。响应时间完全没有变化,如果我调试控制器,我会看到它每次都被请求击中。

启动类ConfigureServices(以及其他内容):

services.AddResponseCaching();

启动类Configure(以及其他内容):

 app.UseResponseCaching();

 app.UseMvc();

控制器动作:

    [HttpGet("{employeeNr}", Name = EmployeeAction)]
    [ResponseCache(Duration = 50)]
    [Produces(typeof(EmployeeDto))]
    public async Task<IActionResult> GetEmployee(SpecificEmployeeParameters parameters)
    {
        if (!await _employeeRepository.EmployeeExists(parameters.EmployeeNr)) return NotFound();

        if (!_typeHelperService.TypeHasProperties<EmployeeDto>(parameters.Fields))
            return BadRequest();

        var entity = await _employeeRepository.GetEmployee(parameters.EmployeeNr);

        var result = Mapper.Map<EmployeeDto>(entity);
        return Ok(result.ShapeData(parameters.Fields));
    }

来自邮递员的响应标头:

cache-control →private,max-age=50
content-type →application/json; charset=utf-8
date →Wed, 30 Aug 2017 11:53:06 GMT
吐司先生

解决了该问题。上面的代码很好!邮递员通过设置来阻止缓存。

要解决此问题,请转到邮递员设置:

General -> Headers -> Send no-cache header -> Set to "OFF"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Web API的AuthorizeAttribute(ASP.NET Core 2)

asp.net core 2 Web API超时问题

ASP.NET Core 2 WEB API-依赖注入

ASP.NET Core Web API - 测量响应数据

设置Web API响应的默认格式(ASP.NET Core)

从ASP .NET Core Web API方法返回完整的XML响应

无法从asp.net core web api获得角度响应

从ASP.NET Web API ASP.NET Core 2返回HTML并获得http状态406

为什么 ASP.NET Web API 2 和 ASP.NET Core 3.1 Web API 之间的行为不同?

调试asp.net core web API

Asp Net Core Web Api POST Request.Body 总是长度为 0

使用Razor从ASP.Net Core 2 Web App调用Web API

ASP.NET Core WebAPI:内存缓存与响应缓存

可以在Windows服务定位中托管ASP.NET Core 2 Web API NET Core 2应用程序

ASP.NET Core 2.1 API缓存

在ASP.NET Core Web API中实现HTTP缓存(ETag)

ASP.NET Core-Web API中SPA路由的缓存问题

ASP.NET Core缓存X时间的外部Web调用

如何将XML从ASP.NET 4 Web API返回到ASP.NET Core 2应用程序?

ASP .Net Core Web 应用程序调用 Web API

如何从Web API调用ASP.NET Core Web MVC

如何在ASP.NET Core 2中对Facebook Web API进行身份验证

Asp.Net Core 2 Web API 500内部服务器错误

ASP.Net Core 2.1 Web Api 2和Active Directory凭据

从 Angular 2 前端到 Asp.Net Core web api 的 JSON 数据发布 - 丢失值

从 ASP.NET Core Web API 应用程序访问 OAuth2 端点

如何从ASP.NET Core RC2 Web Api返回HTTP 500?

asp.net core 2 web api 从 appsettings 设置托管 url

ASP.NET Core Web API + Angular 2授权和身份验证