使swashbuckle中的参数可选(不是必需的)

Max*_*man 9 swagger asp.net-core

我想在我的控制器中将param作为可选项,但是swagger根据需要显示它.

我的控制器看起来像:

[HttpGet("{name}")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicy(string name)
 {
     if (string.IsNullOrEmpty(name))
     {
         return BadRequest("Name is empty");
     }
     try
     {
         CRUDPolicyResponse crudPolicyResponse = await _managementOperations.CRUDPolicy(CRUDType.Read, name, null);
         if (!crudPolicyResponse.OperationSucceeded)
         {     
            return StatusCode((int)HttpStatusCode.BadRequest, crudPolicyResponse.Message);
         }
         if (crudPolicyResponse.FileMetadataPolicy == null)
         {
             return NotFound($"Policy name doesn't exists NAME: {name}");
         }
         return Ok(crudPolicyResponse.FileMetadataPolicy);
     }
     catch (Exception ex)
     {
        _log.Error("Error while trying to save file meta data policy", ex);
            return StatusCode((int)HttpStatusCode.InternalServerError, ex);
     }
 }
Run Code Online (Sandbox Code Playgroud)

我试图像这样更改为默认值:string name = null但不工作/ swashbuckle看起来像这样

所以名称字符串是必需的,我不能使用名称为空.

我试图通过这个解决方案来解决我的问题, 使得int可以为空

Ram*_*Ram -1

尝试一个DefaultValue属性。

在你的情况下,那就是:

[DefaultValue("")]
Run Code Online (Sandbox Code Playgroud)