簡體   English   中英

iServiceCollection' 不包含 'addVersionedApiExplorer' 的定義

[英]iServiceCollection' does not contain a definition for 'addVersionedApiExplorer'

在 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

根據 NSwag 的文檔,我意識到我必須將以下內容添加到我的 '''Startup.ConfigureServices()''' 中:

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

但是 '''AddVersionedApiExplore()''' 在那里不可用...然后我找到了這個 ApiExplorerOptions wiki ,它指出(我是這樣理解的)自 Asp.Net Core 3.0 以來,它的用法是:

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

但我最終得到錯誤'IServiceCollection'不包含'AddVersionedApiExplorer'的定義

順便說一句,對於每個版本,服務的所有路徑都從 Postman 開始運行良好,swagger 的頁面加載並顯示了我聲明的兩個版本,但它無法加載它們的定義。

有人可以指出我正確的方向嗎?

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

.Net 6開始,package 現在稱為Asp.Versioning.Mvc.ApiExplorer

相同的 package 在.Net 7中使用不同的實現。

使用構建應用程序的新最小方法:


var apiVersioningBuilder = builder.Services.AddApiVersioning(options =>
{
    options.ReportApiVersions = true;
    options.DefaultApiVersion = new ApiVersion(1, 0);
    options.AssumeDefaultVersionWhenUnspecified = true;
    // Use whatever reader you want
    options.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
                                    new HeaderApiVersionReader("x-api-version"),
                                    new MediaTypeApiVersionReader("x-api-version"));
}); // Nuget Package: Asp.Versioning.Mvc

apiVersioningBuilder.AddApiExplorer(options =>
{
    // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
    // note: the specified format code will format the version as "'v'major[.minor][-status]"
    options.GroupNameFormat = "'v'VVV";

    // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
    // can also be used to control the format of the API version in route templates
    options.SubstituteApiVersionInUrl = true;
}); // Nuget Package: Asp.Versioning.Mvc.ApiExplorer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM