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

Ole*_* Sh 7 swagger swashbuckle asp.net-core

我有一个带有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)
    {
Run Code Online (Sandbox Code Playgroud)

我想根据需要标记查询参数。我该怎么做?注意,我在查询字符串中传递参数,而不是在正文中等

ESG*_*ESG 16

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

public async Task<IActionResult> SetToken([FromQuery, BindRequired]string token)
Run Code Online (Sandbox Code Playgroud)

  • 这至少需要 .Net Core 2.1 才能使“ModelState.IsValid”正常运行,请参阅:https://andrewlock.net/coming-in-asp-net-core-2-1-top-level-mvc-参数验证/ (2认同)