Swashbuckle.AspNetCore:“规范中没有定义任何操作!” 'Microsoft.AspNetCore.Mvc.ApiExplorer' 包更新到 2.2.0 后的问题

Eri*_*kas 7 swashbuckle asp.net-core

我们有使用 Swagger 的 .net core 2.1 mvc webapi 项目。我们使用以下软件包:

<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="4.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
Run Code Online (Sandbox Code Playgroud)

swashbuckle 的配置方式如下:

services.AddMvcCore().AddVersionedApiExplorer();
services.AddApiVersioning();
services.AddSwaggerGen();
Run Code Online (Sandbox Code Playgroud)

在这种设置/swagger/v1/swagger.json中一切正常(具有所有操作和定义,并且 UI 呈现正确 -> 具有所有控制器和动作等)

我们正在尝试将我们的 .net 核心项目从 2.1 迁移到 2.2 .net 核心。

为了做到这一点(没有警告),我们需要将Microsoft.AspNetCore.Mvc.ApiExplorernuget 从 2.1.2升级到 2.2.0。

在此 nuget 更新之后 swagger.json ( /swagger/v1/swagger.json) 不包含任何"paths": {}and"definitions": {}这导致 swagger UI 不显示控制器/操作(它呈现:No operations defined in spec!

升级包后,这些包版本更新:

<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.2.0" /> //was 2.1.2
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> //was 2.1.1
Run Code Online (Sandbox Code Playgroud)

我尝试将 swashbuckle 版本升级到 v4.0.0,但没有解决问题。

我还缺少哪些包需要更新,所以swagger.json在使用Microsoft.AspNetCore.Mvc.ApiExplorer 2.2.0包时会正确生成?还是我错过了其他东西?

小智 10

刚刚遇到了同样的问题,我想我也会分享我的解决方案。版本 6.0.4。

我必须将 [ApiController] 属性添加到我的控制器中。Swagger 必须使用它来进行发现。


Eri*_*kas 2

我找到了如何解决这个问题(也发布到:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1166#issuecomment-497800414

简而言之:我需要更改我的主机项目 SDK:从“Microsoft.NET.Sdk”更改为“Microsoft.NET.Sdk.Web”。

我没有明确发现它应该是这样的(但默认的 Microsoft 模板在 .net core 2.2 中使用此 SDK)

因此,如果有人能解释为什么从 .net core 2.1 迁移到 2.2 后需要使用 .Web SDK,那就太好了