Leverage MultipleApiVersions in Swagger with attribute versioning

后端 未结 2 1831
一生所求
一生所求 2021-01-04 14:06

Is it possible to leverage MultipleApiVersions in Swagger UI / Swashbuckle when using attribute routing?

Specifically, I implemented versioning by:



        
相关标签:
2条回答
  • 2021-01-04 14:53
    .EnableSwagger(c => c.MultipleApiVersions(
            (apiDesc, version) =>
            {
                var path = apiDesc.RelativePath.Split('/');
                var pathVersion = path[1];
    
                return CultureInfo.InvariantCulture.CompareInfo.IndexOf(pathVersion, version, CompareOptions.IgnoreCase) >= 0;
            },
            vc =>
            {
                vc.Version("v2", "Swashbuckle Dummy API V2"); //add this line when v2 is released
    
                // ReSharper disable once ConvertToLambdaExpression
                vc.Version("v1", "Swashbuckle Dummy API V1");
            }
            ))
    
    0 讨论(0)
  • 2021-01-04 15:07

    Swagger supports multiple versions. Configure the URL such that Swagger can specify the version correctly.

    httpConfiguration
    .EnableSwagger(c =>
        {
            c.MultipleApiVersions(
                (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
                (vc) =>
                {
                    vc.Version("v2", "Swashbuckle Dummy API V2");
                    vc.Version("v1", "Swashbuckle Dummy API V1");
                });
        });
    .EnableSwaggerUi(c =>
        {
            c.EnableDiscoveryUrlSelector();
        });
    
    
        private static bool ResolveVersionSupportByRouteConstraint(ApiDescription apiDesc, string targetApiVersion)
        {
            return apiDesc.ActionDescriptor.ControllerDescriptor.ControllerType.FullName.Contains($"{targetApiVersion}.");
        }
    

    References:

    • https:https://github.com/domaindrivendev/Swashbuckle#describing-multiple-api-versions
    0 讨论(0)
提交回复
热议问题