首页 > 解决方案 > 路径中的 Swashbuckle 参数强制为必需

问题描述

我遇到了一个问题,如果我试图强制 swashbuckle 理解参数是路径的一部分,它会强制它为必需的。

[HttpGet]
[Route("test/{format}/{baseKey?}")]
[Route("test/{format}/{baseKey}/{apiKey}/{year}/{code}/{month}/{includeImages?}")]
[Produces("application/json", "application/xml")]
public async Task<List<TestItem>> Test([FromRoute] string format, [FromRoute] string baseKey, [FromRoute] string apiKey, [FromRoute] string year, [FromRoute] string code, [FromRoute] string month, [FromRoute] bool includeImages = false)

但即使是带有标记的项目= false仍然是按要求标记的。除此之外,.net 核心?在其路由中用于定义非必需项,所以我有点困惑为什么 Swashbuckle 不尊重这一点。

有任何想法吗?

标签: c#asp.net-coreswashbuckleswashbuckle.aspnetcore

解决方案


我已经翻阅了swashbuckle 的文档,并确定我需要Swashbuckle.AspNetCore.Annotations从 NuGet 安装,然后将其添加到 startup.cs:

services.AddSwaggerGen(c =>
{
   ...
   c.EnableAnnotations();
});

然后执行类似以下操作:

[HttpGet]
public IActionResult GetProducts([FromQuery, SwaggerParameter("Search keywords", Required = true)] string keywords)

推荐阅读