【问题标题】:How to do the model binding of deep objects from open-api doc in asp.net core 3.1如何在asp.net core 3.1中从openapi doc做深度对象的模型绑定
【发布时间】:2021-11-06 01:20:43
【问题描述】:

如何在 asp.net core 3.1 中绑定开放 API 文档中存在的深层对象?

我要解析的请求是:

GET https://localhost:5001/Product/?product[name]=mac&product[type]=computer

见下方控制器以及产品型号

[ApiController]
[Route("[controller]")]
public class DevicesController : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> Get([FromQuery]ProductQuery productQuery)
    {
        var response = await _mediator.Send(productQuery);
        return Ok(response);
    }

}


public class ProductQuery: IRequest<IEnumerable<ProductDto>>
{
   public string Name {get; set;}
   public string Type {get; set;}
}

【问题讨论】:

标签: c# asp.net-core asp.net-core-mvc swagger openapi


【解决方案1】:

ASP.NET Core 不支持绑定到以这种方式定义的开箱即用的查询字符串参数。您将不得不编写一个自定义模型绑定器来实现这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多