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

后端 未结 2 1430
醉梦人生
醉梦人生 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:31

    ValueV1Controller.cs

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

    ValueV2Controller.cs

    [RoutePrefix("api/v2/value")]
    public class ValueV2Controller : ApiController
    {
        [Route("get")]
        public IEnumerable 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();
                });
        }
    }
    

提交回复
热议问题