我想在我的 Controller 中将参数设为可选,但 swagger 根据需要显示它。
我的 Controller 看起来像:
[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);
}
}
我尝试更改为这样的默认值:
string name = null
但不工作/所以名称字符串是必需的,我不能让名称为空。
我试图用这个解决方案解决我的问题
make int as nullable
最佳答案
将默认值添加到您的 Controller 参数
[HttpGet("{name}")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicy(string name = "")
{
...
}
关于asp.net-core - 使 swashbuckle 中的参数可选(非必需),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46510901/