gpt4 book ai didi

asp.net-core - 使 swashbuckle 中的参数可选(非必需)

转载 作者:行者123 更新时间:2023-12-04 04:36:59 25 4
gpt4 key购买 nike

我想在我的 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但不工作/
swashbuckle looks like this

所以名称字符串是必需的,我不能让名称为空。

我试图用这个解决方案解决我的问题
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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com