如何使用Swashbuckle在Swagger中传递复杂对象作为查询字符串

67up9zun  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(198)

我有一个复杂的对象要在ASP.NETCore3.2中作为查询字符串传递。

public class Customer
{
    public string Firstname { get; set; }
    public List<Address> Addresses { get; set; }
}

public class Address
{
    public string Home_Address { get; set; }
    public string Office_Address { get; set; }
}

它在Postman中的工作原理如下:

http://localhost:52100/v1/Customer?Addresses[0].Home_Address=123HomeStreet&Addresses[0].Office_Address=123OfficeStreet

但是,如何在Swagger文档中为Addresses传递类型为“array[object](query)”的值http://localhost:52100/v1/swagger-ui/index.html
我在我的项目中添加了Swashbuckle.AspNetCore 5.4.1、Swashbuckle.AspNetCore.Annotations 5.4.1、Swashbuckle.AspNetCore.Filters 5.1.1引用

t40tm48m

t40tm48m1#

“获取”请求不是此类查询的行业标准。这里有一个包含N个地址的列表,所以你应该使用带有参数属性[FromBody]的“Post”请求

[HttpPost]
public async Task<ActionResult> PostAsync([FromBody] Customer customerObj, CancellationToken cancellationToken)
{
// your code
}

身体:

{
  "FirstName": "John",
  "Adresses": [
    {
      "Address": {
        "Home_Address": "WorkDrive 11",
        "Office_Address": "HomeDrive 22"
      }
    }
  ]
}

相关问题