简体   繁体   English

为什么 Swashbuckle.aspnet.core.swagger 不被识别

[英]Why Swashbuckle.aspnet.core.swagger not being recognized

I've installed though nuget package manager the Swashbuckle.AspNetCore.Swagger and had included the using Swashbuckle.AspNetCore.Swagger on the top, but I get error on the Info{ Title} methods. I've installed though nuget package manager the Swashbuckle.AspNetCore.Swagger and had included the using Swashbuckle.AspNetCore.Swagger on the top, but I get error on the Info{ Title} methods. Can anyone please suggest how to solve this issue.谁能建议如何解决这个问题。

 services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My DAB API", Version = "V3.2.2" });
            }); 

I find Solution.我找到解决方案。

For version 5对于版本 5

using Microsoft.OpenApi.Models;


services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});

If you are using DotnetCore (>= 3.0) Make sure you are using Pre-Release (as of now Dec-19) of Swashbuckle.AspNetCore package which is -Version 5.0.0-rc4 .如果您使用的是 DotnetCore (>= 3.0) 确保您使用的是Swashbuckle.AspNetCore包的预发布版(截至 12 月 19 日),即-Version 5.0.0-rc4 I mistakenly installed the latest stable version 4.0.1 in which Microsoft.OpenApi.Models was missing.我错误地安装了最新的稳定版本4.0.1 ,其中缺少Microsoft.OpenApi.Models

See More On Microsoft 查看更多关于微软

I had same issue in ASP.NET Core 2.1 web api but I have resolved issue by first installing the lower version of package & upgrading it version by version我在 ASP.NET Core 2.1 web api 中遇到了同样的问题,但我通过首先安装较低版本的包并逐个版本升级它来解决问题

Swashbuckle.AspNetCore 2.1.0 and then after I have upgraded the package version by version until I got the same error but I haven't received same error again even upgrading till swashbuckle.AspNetCore 5.0.0-rc2 & its now working properly with following configuration Swashbuckle.AspNetCore 2.1.0 然后在我逐个版本升级包版本之后,直到出现相同的错误,但我没有再次收到相同的错误,即使升级到 swashbuckle.AspNetCore 5.0.0-rc2 并且它现在可以正常使用以下内容配置

Use namespace as following because Info class is not more used in latest version of swagger instead of OpenApiInfo class used to describe about swagger metadata使用命名空间如下,因为 Info 类在最新版本的 swagger 中不再使用,而不是用于描述 swagger 元数据的 OpenApiInfo 类

using Microsoft.OpenApi.Models;

then use the following code in Startup.cs class under the ConfigureServices method然后在ConfigureServices方法下的Startup.cs类中使用以下代码

 services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "www.compilemode.com", Version = "v1" });
        });

This is very strange resolution but this is the only work around I have found after spending lots of time.这是一个非常奇怪的解决方案,但这是我花了很多时间找到的唯一解决方法。

卸载包Swashbuckle.AspNetCore.Swagger并尝试这个包Swashbuckle.AspNetCore.SwaggerGen

Well, I found the solution, Finally.好吧,我找到了解决方案,终于。 I installed Swashbuckle.AspNetCore version 4.0.1.我安装了 Swashbuckle.AspNetCore 版本 4.0.1。 Obviously it wasn't working with the latest version which is 5.显然它不适用于最新版本 5。

while upgrading project to .net core 5.0 or 6.0, SwaggerDoc does have the SwaggerDoc("v1", info), instead of use SwaggerDoc("v1", new OpenApiInfo());在将项目升级到 .net 核心 5.0 或 6.0 时,SwaggerDoc 确实有 SwaggerDoc("v1", info),而不是使用 SwaggerDoc("v1", new OpenApiInfo());

只需安装软件包 Microsoft.OpenApi 即可解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM