Leverage MultipleApiVersions in Swagger with attribute versioning

拟墨画扇 提交于 2019-11-30 16:55:21

问题


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

Specifically, I implemented versioning by:

using System.Web.Http;

namespace RESTServices.Controllers.v1
{
    [Route("api/v1/Test")]
    public class TestV1Controller : ApiController
    { ... }

Version 2 would be in a v2 namespace. In a controller named TestV2Controller. The route would have v2 in it.

Is it possible to pass a lambda in that will allow this? I found a sample lambda online which compiled, but then Swagger stopped working completely. Couldn't hit breakpoints or see Swagger in the browser.


回答1:


.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");
        }
        ))



回答2:


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();
    });

References:

  • https:https://github.com/domaindrivendev/Swashbuckle#describing-multiple-api-versions


来源:https://stackoverflow.com/questions/30789045/leverage-multipleapiversions-in-swagger-with-attribute-versioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!