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?
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
Solution: AddVersionedApiExplore lives in Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer NuGet package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With