Asp.net-Core

Swashbuckle.AspNetCore 所需的查詢字元串參數

  • October 29, 2019

我有一個帶有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會有所幫助。

引用自:https://stackoverflow.com/questions/56186603