31

I'm creating a new ASP.NET Core 8 Web API. In my previous projects with .NET 6 or .NET 7, I can use:

services.AddApiVersioning(options =>
{
    options.DefaultApiVersion = new ApiVersion(1, 0);
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.ReportApiVersions = true;
});

services.AddVersionedApiExplorer(options =>
{
    options.GroupNameFormat = "'v'VVV";
    options.SubstituteApiVersionInUrl = true;
});

Now, the package Microsoft.AspNetCore.Mvc.ApiExplorer is deprecated and I replaced it with the new one Asp.Versioning.Mvc.ApiExplorer but there is no extension for AddVersionedApiExplorer.

enter image description here

In the configuration for Swagger, I have the following code:

public class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions>
{
    private readonly IApiVersionDescriptionProvider _provider;

    public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
    {
        _provider = provider;
    }

    public void Configure(SwaggerGenOptions options)
    {
        foreach (var description in _provider.ApiVersionDescriptions)
        {
            options.SwaggerDoc(
                description.GroupName,
                CreateVersionInfo(description));
        }
    }

    public void Configure(string name, SwaggerGenOptions options)
    {
        Configure(options);
    }

    private static OpenApiInfo CreateVersionInfo(
        ApiVersionDescription description)
    {
        var info = new OpenApiInfo()
        {
            Title = "Test API",
            Version = description.ApiVersion.ToString(),
            Description = "This is a test API.",
        };

        if (description.IsDeprecated)
        {
            info.Description += " This API version has been deprecated.";
        }

        return info;
    }
}

When I run the application, I get this error

Unable to resolve service for type 'Asp.Versioning.ApiExplorer.IApiVersionDescriptionProvider' while attempting to activate 'Middletier.Api.Extensions.Configuration.ConfigureSwaggerOptions'.

enter image description here

How can I fix it?

2 Answers 2

49

AddApiVersioning works a bit different (it returns IApiVersioningBuilder). Install Asp.Versioning.Mvc.ApiExplorer which will allow to call AddApiExplorer on IApiVersioningBuilder returned by AddApiVersioning:

builder.Services.AddApiVersioning(options =>
    {
        options.DefaultApiVersion = new ApiVersion(1, 0);
        options.AssumeDefaultVersionWhenUnspecified = true;
        options.ReportApiVersions = true;
    })
    .AddApiExplorer(options =>
    {
        options.GroupNameFormat = "'v'VVV";
        options.SubstituteApiVersionInUrl = true;
    });

See the Quick Start, samples and this question - ApiVersion attribute not working in .NET 8

Sign up to request clarification or add additional context in comments.

2 Comments

Soo, I've got a different issue. If I remove .AddApiExplorer, I am able to access openapi/v1.json and see my endpoints. However, if I add that particular part, I can't access it anymore. I have my GroupNameFormat set as "v'V'" but I don't understand why I can't access the OpenApi anymore nor what I should set to access it.
6

In .NET8 I'm using very simple steps to achieve similar result.

First I'm adding the API explorer.

builder.Services.AddEndpointsApiExplorer();

Then I'm using IActionDescriptorCollectionProvider or IApiDescriptionGroupCollectionProvider for the swagger configs. I'm using the latest version(8.0.0 atm) of Asp.Versioning.Mvc.ApiExplorer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.