Swagger not working correctly with multiple versions of ASP.NET WebApi app

后端 未结 2 1419
醉梦人生
醉梦人生 2020-12-31 06:07

Please help me with this, it looked easy at first, now I\'m late in the project:

I\'m trying to setup API versioning for a ASP.NET WebApi project, along with Swagger

相关标签:
2条回答
  • 2020-12-31 06:11

    Solved it by:

    1. Adding the Microsoft.AspNet.WebApi.Versioning.ApiExplorer package
    2. Using the versioned API explorer as below (note that I had to move the code from SwaggerConfig.cs in WebApiConfig.cs due to initialization issues):

          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", swagger => {
                  swagger.OperationFilter<SwaggerDefaultValues>();
                  swagger.MultipleApiVersions(versionSupportResolver, versionInfoBuilder);
                  swagger.IncludeXmlComments(WebApiConfig.XmlCommentsFilePath);
              })
              .EnableSwaggerUi(swaggerUi => {
                  swaggerUi.EnableDiscoveryUrlSelector();
                  swaggerUi.DocExpansion(DocExpansion.List);
              });
      
    0 讨论(0)
  • 2020-12-31 06:31

    ValueV1Controller.cs

    [RoutePrefix("api/v1/value")]
    public class ValueV1Controller : ApiController
    {
        [Route("get")]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
    

    ValueV2Controller.cs

    [RoutePrefix("api/v2/value")]
    public class ValueV2Controller : ApiController
    {
        [Route("get")]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1.2", "value2.2" };
        }
    }
    

    SwaggerConfig.cs

    public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;
    
            GlobalConfiguration.Configuration
                .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");
                            vc.Version("v1", "Swashbuckle Dummy API V1");
                        });
                })
                .EnableSwaggerUi(c =>
                {
                    c.EnableDiscoveryUrlSelector();
                });
        }
    }
    
    0 讨论(0)
提交回复
热议问题