【问题标题】:'Info' class not recognized with Swashbuckle and Swagger librariesSwashbuckle 和 Swagger 库无法识别“信息”类
【发布时间】:2019-10-23 11:10:40
【问题描述】:

我是否因为我的AddSwaggerGen 方法无法编译而缺少包或参考。我的代码:

我加了using Swashbuckle.AspNetCore.Swagger;

但是这段代码

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

不会编译:

错误 CS0246 找不到类型或命名空间名称“Info”(是 您缺少 using 指令或程序集引用?) 这是csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IdentityModel" Version="3.10.6" />
    <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc2" />
    <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc2" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc2" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc2" />
  </ItemGroup>

【问题讨论】:

  • 请上传您的csproj 文件或列出您的PackageReference
  • 我已经添加了我的 .csproj
  • 一切似乎都很好,恐怕我对此一无所知:|
  • 尝试将其更改为c.SwaggerDoc("v1", new OpenApiInfo{ Title = "My API", Version = "v1" });

标签: c# asp.net-core swagger


【解决方案1】:

Info 在版本 5 中已弃用

它已被OpenApiInfo 取代,因此您必须在您的招摇设置中反映这一点:

c.SwaggerDoc("My_Api", new OpenApiInfo { Version = "1.0", Description = "my api " });

【讨论】:

    【解决方案2】:

    将 Info 类更改为 OpenApiInfo 类。

    使用 Microsoft.OpenApi.Models 添加

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多