ASP.NET Core 2.0 Web API响应缓存

编程入门 行业动态 更新时间:2024-10-26 08:23:03
本文介绍了ASP.NET Core 2.0 Web API响应缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to implement response caching in my web api using the "Microsoft.AspNetCore.ResponseCaching" package.

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

I am using Postman to test the application and validate headers etc.

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

The correct headers are generated with the specified max-age but postman seems to request the response from the api and not using the cache. The response time does not vary at all and if I debug the controller I can see that it gets hit by the request every time.

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

Startup class ConfigureServices (along with other stuff):

services.AddResponseCaching();

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

Startup class Configure (along with other stuff):

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

推荐答案

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

Fixed the issue. The code above is alright! Postman was preventing caching by a setting.

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

To fix this behaviour go to Postman Settings:

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

更多推荐

ASP.NET Core 2.0 Web API响应缓存

本文发布于:2023-11-08 11:01:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   Core   NET   ASP   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!