【问题标题】:iServiceCollection' does not contain a definition for 'addVersionedApiExplorer'iServiceCollection 不包含“addVersionedApiExplorer”的定义
【发布时间】:2020-10-01 05:45:56
【问题描述】:

在 Asp.Net Core v3.1 api、Swagger v3.0 中,当我声明多个版本时,Swagger UI 无法加载 API 定义。

编辑:同样来自 NuGet:

Mcrosoft.AspNetCore.Mvc.Versioning v4.1.1

NSwag.AspNetCore v13.7.0

Following the documentation of NSwag 我意识到我必须在我的 '''Startup.ConfigureServices()''' 中添加以下内容:

services.AddApiVersioning(options =>
{
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.ApiVersionReader = new UrlSegmentApiVersionReader();
})
.AddMvcCore()
.AddVersionedApiExplorer(options =>
{
    options.GroupNameFormat = "VVV";
    options.SubstituteApiVersionInUrl = true;
});

但是 '''AddVersionedApiExplore()''' 在那里不可用...然后我发现 this ApiExplorerOptions wiki 它声明(我理解)自 Asp.Net Core 3.0 以来它的用法是:

services.AddVersionedApiExplorer( options => { /* configure options */ } );

但我最终得到错误'IServiceCollection' 不包含'AddVersionedApiExplorer' 的定义

顺便说一句,每个版本的 Postman 服务的所有路径都运行良好,swagger 的页面加载并显示了我声明的两个版本,但它无法加载它们的定义。

有人可以指点我正确的方向吗?

【问题讨论】:

    标签: c# asp.net-core swagger nswag


    【解决方案1】:

    解决方案:AddVersionedApiExplore 位于 Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer NuGet 包中。

    【讨论】:

    • Lupa,只是想在此处添加注释。忘记包含一个 nuget 包 - 当您在项目中使用所述包时,这不完全是错误吗?这就像去电影院并意识到你忘记带票一样。只是一些思考的食物:)
    • 谢谢@Jay,我的错是信任NuGetRecommender 扩展。我花了几个小时,当我输入 services.AddVersionedApiExplorer() 时,它并没有像在其他类似情况下那样建议安装任何包。很抱歉:/
    • 啊,我明白了。现在我全面了解了你是如何陷入这种境地的。
    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 2022-01-03
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 2019-04-14
    • 2016-09-25
    • 2020-08-20
    相关资源
    最近更新 更多