【问题标题】:Where does this "Required" attribute for the Swagger UI coming from for ASP.NET?ASP.NET 的 Swagger UI 的“必需”属性从何而来?
【发布时间】:2022-01-25 08:38:12
【问题描述】:

我的 ASP.NET 控制器中有以下内容:

[Produces("application/json")]
[Consumes("application/json")]
[Route("api/[controller]")]
[ApiController]
public class ConnectionManagersController : ControllerBase 
{
    [HttpGet("{connectionManagerID:int}")]
    [ProducesResponseType(typeof(ConnectionManagerModel), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int connectionManagerdID){}
}

但是,当我运行应用程序并出现 Swagger UI 时,我会看到以下屏幕:

Swagger UI 上有两个 connectionManagerID 字段 - 第一个是 int(应该是),第二个是字符串,是必需的,我不知道它来自哪里。

我不知道 *required 字段来自哪里。

【问题讨论】:

    标签: asp.net-core swagger-ui


    【解决方案1】:

    参数connectionManagerdIDint - 它不能为空,因此实际上它是必需的。

    【讨论】:

      【解决方案2】:

      试试这个:

      public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int connectionManagerdID = null){}  
      

      它应该设置为可选。

      【讨论】:

      • Swagger UI 上有两个 connectionManagerID 字段 - 第一个是 int(应该是),第二个是字符串,是必需的,我不知道它来自哪里。跨度>
      【解决方案3】:

      它来自这里,如果你不向 url 提供 id,你可能会得到 404。

      [HttpGet("{connectionManagerID:int}")]
      

      如果你不想被要求试试这个

      [HttpGet("{connectionManagerID?:int}")]
      

      在这种情况下,也许改变动作也是有意义的

      public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int? connectionManagerdID){}  
      

      或者你可以保留你所拥有的。默认为0。

      【讨论】:

        【解决方案4】:

        我有两个参数拼写不同。 :(

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-08
          • 2021-12-28
          • 2011-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多