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

标签 asp.net-core swagger

我想在我的 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/

相关文章:

asp.net-core - 将 Microsoft.EntityFrameworkCore.Tools 添加到 ASP.NET Core 项目

c# - Asp.Net Core 中的@Html.Action

asp.net-core - VSTS - 构建 ASP.NET Core 2.0,编译错误 : Could not locate the assembly "Microsoft.AspNetCore.Mvc.ViewFeatures"

javascript - Swagger 用户界面 : basic auth for swagger file

django - 为什么我收到 django Rest Framework swagger 的 "no module named urls"错误?

json - 如何绕过@JsonIgnore注解?

c# - 在 .net 核心项目中按环境加载 WCF 服务

asp.net-core - Azure管道发布: task DotNetCoreCLI with specific folder or project

java - 试图调用不存在的方法。尝试是从以下位置 : 进行的

自动生成的 Node.js 服务器无法启动 (Swagger/OpenAPI)