iServiceCollection' does not contain a definition for 'addVersionedApiExplorer'
Asked Answered
T

2

18

In an Asp.Net Core v3.1 api, Swagger v3.0, the Swagger UI cannot load API definitions when I have declared multiple versions.

Edit: Also from NuGet:

Mcrosoft.AspNetCore.Mvc.Versioning v4.1.1

NSwag.AspNetCore v13.7.0

Following the documentation of NSwag I realized that I have to add the following to my '''Startup.ConfigureServices()''':

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

But '''AddVersionedApiExplore()''' is not available there... then I found this ApiExplorerOptions wiki where it states (I understood so) that since Asp.Net Core 3.0 the usage it is:

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

But I endup with the error 'IServiceCollection' does not contain a definition for 'AddVersionedApiExplorer'

BTW, all the paths of the service run fine from Postman for each version, the swagger's page loads and shows both of versions I declared but it cannot load their definitions.

Can somebody please point me to the right direction?

Tunicle answered 30/9, 2020 at 20:37 Comment(0)
B
46

Starting from .Net 6, the package is now called Asp.Versioning.Mvc.ApiExplorer.

the same package uses different implementation in .Net 7.

Using the new minimal method of building an app:


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
Bilow answered 26/12, 2022 at 15:50 Comment(0)
T
12

Solution: AddVersionedApiExplore lives in Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer NuGet package.

Tunicle answered 30/9, 2020 at 21:45 Comment(4)
Lupa, just wanted to add a note here. forgetting to include a nuget package - when you are using said package in your project is not exactly a error is it? Thats like going to the movie theatre and realizing that you forgot to bring the ticket. just some food for thought :)Endothelioma
Thanks @Jay, my fault was trusting in NuGetRecommender extension. I spent several hours and when I typed services.AddVersionedApiExplorer() it did not suggest to install any package as it did in other similar situations. Sorry by that :/Tunicle
ah, i see. now i have the full picture of how you ended up in this situation.Endothelioma
This package Microsoft.AspNetCore.Mvc.Versioning.ApiExploreris deprecated since yesterday. Just use the answer of @Adnan...Prostitution

© 2022 - 2024 — McMap. All rights reserved.