Sae*_*aei 5 c# cancellation-token asp.net-core asp.net-core-webapi
我正在使用 Asp.net Core API 并设置如下服务:
services
.Configure<AppOptions>(_configuration.GetSection("app"))
.AddMvcCore(options =>
{
options.RespectBrowserAcceptHeader = true;
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
})
.AddFormatterMappings()
.AddJsonFormatters()
.AddXmlSerializerFormatters()
.AddCors();
Run Code Online (Sandbox Code Playgroud)
之后,我创建了一个带有 CancellationToken 参数的 API,如下所示:
[HttpGet("list")]
public async Task<ActionResult<IEnumerable<string>>> Get(CancellationToken cancellationToken)
{
var result = await _orderBusinessService.GetList(cancellationToken);
return Ok(result);
}
Run Code Online (Sandbox Code Playgroud)
当我从 Postman 或浏览器调用此 API 时,我得到以下响应:
415 不支持的媒体类型
当我添加[FromQuery]到cancellationToken时,就可以了。
实际上,这似乎CancellationTokenModelBinder行不通。
我不知道为什么?有人有任何想法吗?
请检查您的Startup课程,并确保您设置了兼容性版本:
services.AddMvcCore()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddApiExplorer()
.AddAuthorization();
Run Code Online (Sandbox Code Playgroud)
添加这一行解决了我的问题。(有关将我推向正确方向的评论,请参阅https://github.com/microsoft/aspnet-api-versioning/issues/534#issuecomment-521680394 )。
| 归档时间: |
|
| 查看次数: |
1463 次 |
| 最近记录: |