swagger Swashbuckle.AspNetCore所需的查询字符串参数

idfiyjo8  于 9个月前  发布在  其他
关注(0)|答案(3)|浏览(101)

我有一个ASP.NET Core v2.1项目,包含Swashbuckle.AspNetCore包。我的代码是:

/// <summary>
    /// Set new android token for the current driver
    /// </summary>
    /// <remarks>
    /// Sample request:
    ///
    ///     PUT /SetToken?token=new_token
    ///
    /// </remarks>
    /// <param name="token">can't be null or empty</param>
    /// <returns></returns>
    /// <response code="204">If executed successfully</response>
    /// <response code="400">if token is null or empty</response>  
    /// <response code="404">if user is not a driver; if driver is not found (removed etc); if user does not have a profile</response>  
    [ProducesResponseType(204)]
    [ProducesResponseType(400)]
    [ProducesResponseType(404)]
    [HttpPut]
    [Route("SetToken")]
    [UserIsNotDriverException]
    [NullReferenceException]
    [DriverWithoutProfileException]
    public async Task<IActionResult> SetToken([FromQuery]string token)
    {

字符串
我想将查询参数标记为必需的。我该怎么做呢?注意,我在查询字符串中传递参数,而不是在主体中传递参数

vcirk6k6

vcirk6k61#

可以将BindRequired属性添加到参数。

public async Task<IActionResult> SetToken([FromQuery, BindRequired]string token)

字符串

2nbm6dog

2nbm6dog2#

你可以这样做。

public async Task<IActionResult> SetToken([FromQuery, SwaggerParameter("Token Description", Required = True)]string token)

字符串
使用这个库Swashbuckle.AspNetCore.Annotations会有帮助。

u3r8eeie

u3r8eeie3#

作为这种情况的扩展,如果您将一个复杂的模型(即POCO)绑定到查询参数,并且您需要Swagger要求模型中的某些属性是必需的,则它通过向每个属性添加Required属性来工作。
顺便说一下,在action函数声明中不需要使用BindRequired或类似的东西。

相关问题