1

I am trying to enable versioning for my .NET Framework 4.8 API and also view it in Swagger. I have used the following code. I tried the solution from the following stackoverflow article: Swagger not working correctly with multiple versions of ASP.NET WebApi app. ApiDescriptions returns something for both v1 and v2.

SwaggerConfig.cs/Register

 var thisAssembly = typeof(SwaggerConfig).Assembly;

        var apiExplorer = config.AddVersionedApiExplorer(options => {
            options.GroupNameFormat = "'v'VVV";
        });
        
        var versionSupportResolver = new Func<ApiDescription, string, bool>((apiDescription, version) => apiDescription.GetGroupName() == version);
        
        var versionInfoBuilder = new Action<VersionInfoBuilder>(info => {
            foreach (var group in apiExplorer.ApiDescriptions)
            {
                info.Version(group.Name, $"MyAPI v{group.ApiVersion}");
            }
        });
        
        config
            .EnableSwagger("{apiVersion}/swagger",
                c =>
                {
                    c.PrettyPrint();
                    c.MultipleApiVersions(versionSupportResolver, versionInfoBuilder);
                    c.IncludeXmlComments(XmlCommentsFilePath);
                    c.IgnoreObsoleteActions();
        
                })
            .EnableSwaggerUi(c =>
            {
                c.EnableDiscoveryUrlSelector();
            });

HealthController.cs

[ApiVersion("1.0")]
[RoutePrefix("api/v{version:apiVersion}/health")]
public class HealthController : ApiController
{
    [HttpGet]
    [JwtAuthentication]
    [Route("")]
    public async Task<IHttpActionResult> Check()
    {
        _logger.LogInformation("Checking health of application.");
        if (await _healthService.IsHealthyAsync())
            return Ok("Healthy");

        return InternalServerError();
    }
}

Just for testing purposes I copied the Health Controller and added V2.

HealthTestController.cs

[ApiVersion("2.0")]
[RoutePrefix("api/v{version:apiVersion}/health")]
public class HealthTestController : ApiController
{
    [HttpGet]
    [JwtAuthentication]
    [Route("")]
    public async Task<IHttpActionResult> Check()
    {
        _logger.LogInformation("Checking health of application.");
        if (await _healthService.IsHealthyAsync())
            return Ok("Healthy");

        return InternalServerError();
    }
}

WebApiConfig.cs/Register

            var constraintResolver = new DefaultInlineConstraintResolver() { ConstraintMap = { ["apiVersion"] = typeof(ApiVersionRouteConstraint) } };
            config.AddApiVersioning(options => options.ReportApiVersions = true);
            config.MapHttpAttributeRoutes(constraintResolver);

However, I can't switch between v1 and v2 on the Swagger interface and I'm expected to pass the API version via query. I would like to be able to toggle between V1 and V2 using the drop down.

SwaggerUI

I have already tried everything possible that I have found on the subject and unfortunately get no further.

Thanks & Regards

Helen
  • 87,344
  • 17
  • 243
  • 314

0 Answers0