13

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" });
            }); 

7 Answers 7

19

I find Solution.

For version 5

using Microsoft.OpenApi.Models;


services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
1
  • If need Contact inside of OpenApiInfo then it too needs to be OpenApiContact -> new OpenApiContact { Contact = new OpenApiContact { Name = "", Email = "" }}
    – Ben
    Commented Feb 4, 2020 at 21:49
7

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. I mistakenly installed the latest stable version 4.0.1 in which Microsoft.OpenApi.Models was missing.

See More On Microsoft

1

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

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

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

using Microsoft.OpenApi.Models;

then use the following code in Startup.cs class under the ConfigureServices method

 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.

1
0

Uninstall package Swashbuckle.AspNetCore.Swagger and try this package Swashbuckle.AspNetCore.SwaggerGen.

0

Well, I found the solution, Finally. I installed Swashbuckle.AspNetCore version 4.0.1. Obviously it wasn't working with the latest version which is 5.

-1

Just install package Microsoft.OpenApi to resolve the issue

-1

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());