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

Rak*_*tha 5 c# http-get swagger swashbuckle asp.net-core

我有一个复杂的对象要在 ASP.Net Core 3.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; }
}
Run Code Online (Sandbox Code Playgroud)

它在 Postman 中的工作方式如下:

http://localhost:52100/v1/Customer?Addresses[0].Home_Address=123HomeStreet&Addresses[0].Office_Address=123OfficeStreet
Run Code Online (Sandbox Code Playgroud)

但是,如何在地址类型为“array[object](查询)”的 Swagger 文档中传递值 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 引用

小智 0

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

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

与身体:

{
  "FirstName": "John",
  "Adresses": [
    {
      "Address": {
        "Home_Address": "WorkDrive 11",
        "Office_Address": "HomeDrive 22"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)