将swashbuckle中的参数设为可选(不需要) [英] Make parameters in swashbuckle optional(not required)
本文介绍了将swashbuckle中的参数设为可选(不需要)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在控制器中将param设置为可选参数,但会大摇大摆地根据需要显示它.
I want to make param in my controller as optional but swagger shows it as required.
我的控制器看起来像:
[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
,但无法正常工作/
I tried to change to default value like this: string name = null
but not working/
因此,名称字符串是必需的,我无法使名称为空.
So the name string is required and i can't make get with name as empty.
我试图用此解决方案解决我的问题 将int设为可为空
I tried to solve my problem with this solution make int as nullable
推荐答案
将默认值添加到控制器参数
Add the default value to your controller parameter
[HttpGet("{name}")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicy(string name = "")
{
...
}
这篇关于将swashbuckle中的参数设为可选(不需要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文