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

后端 未结 2 1438
醉梦人生
醉梦人生 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, version) => apiDescription.GetGroupName() == version);
      
          var versionInfoBuilder = new Action(info => {
              foreach (var group in apiExplorer.ApiDescriptions)
              {
                  info.Version(group.Name, $"MyAPI v{group.ApiVersion}");
              }
          });
      
          config
              .EnableSwagger("{apiVersion}/swagger", swagger => {
                  swagger.OperationFilter();
                  swagger.MultipleApiVersions(versionSupportResolver, versionInfoBuilder);
                  swagger.IncludeXmlComments(WebApiConfig.XmlCommentsFilePath);
              })
              .EnableSwaggerUi(swaggerUi => {
                  swaggerUi.EnableDiscoveryUrlSelector();
                  swaggerUi.DocExpansion(DocExpansion.List);
              });
      

提交回复
热议问题