Search code examples
asp.net-coreswaggerswashbuckle

Swashbuckle.AspNetCore required query string parameter


I have an ASP.NET Core v2.1 project with Swashbuckle.AspNetCore package. My code is:

    /// <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)
    {

I want to mark query parameter as required. How can I do it? Pay attention, I pass parameter in query string, not inside body etc


Solution

  • You can add the BindRequired attribute to your parameter.

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