dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.4k stars 10k forks source link

Microsoft.AspNetCore.OpenApi How to setup versioning in .net9 using Microsoft OpenApi with minimal-api #56994

Closed AleksandarDevic closed 3 months ago

AleksandarDevic commented 3 months ago

With new template(webapi) in .NET9 - ASP.NET Core Swagger build-in is removed.

Can anyone show me/help me how to implement API versioning using Microsoft OpenApi with minimal-api.

Some part of configuration is: ApiVersionSet apiVersionSet = app.NewApiVersionSet() .HasApiVersion(new ApiVersion(1)) .ReportApiVersions() .Build();

var routeGroupBuilder = app.MapGroup("api/v{version:apiVersion}").WithApiVersionSet(apiVersionSet);

Thanks in advance. :-)

AleksandarDevic commented 3 months ago

My fault. Everyting works.

public static class DependencyInjection { public static IServiceCollection AddInfrastructure( this IServiceCollection services, IConfiguration configuration) { AddApiVersioning(services);

    return services;
}

private static void AddApiVersioning(IServiceCollection services)
{
    services
        .AddApiVersioning(options =>
        {
            options.DefaultApiVersion = new ApiVersion(1);
            options.ReportApiVersions = true;
            options.ApiVersionReader = new UrlSegmentApiVersionReader();
        })
        .AddMvc()
        .AddApiExplorer(options =>
        {
            options.GroupNameFormat = "'v'VVV";
            options.SubstituteApiVersionInUrl = true;
        });
}

}