我有一个带有
Swashbuckle.AspNetCore
包的 ASP.NET Core v2.1 项目。
我的代码是:
/// <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)
{
我想根据需要标记查询参数。我该怎么做?注意,我在查询字符串中传递参数,而不是在正文等内部传递参数
您可以将 BindRequired 属性添加到您的参数中。
public async Task<IActionResult> SetToken([FromQuery, BindRequired]string token)
你可以这样做。
public async Task<IActionResult> SetToken([FromQuery, SwaggerParameter("Token Description", Required = True)]string token)
使用这个库
Swashbuckle.AspNetCore.Annotations
会有帮助。
作为这种情况的扩展,如果将复杂模型(即 POCO)绑定到查询参数,并且需要 Swagger 要求模型内的某些属性是必需的,则可以通过向每个属性添加
Required
属性来工作.
顺便说明一下,无需在操作函数声明中使用
BindRequired
或类似内容。