如何使用aspnet ApiVersioning在Swashbuckle中配置MultipleApiVersions

时间:2017-06-05 19:28:24

标签: c# asp.net-web-api swagger swashbuckle

如何配置client = boto3.client('s3', region_name='us-west-2') paginator = client.get_paginator('list_objects') operation_parameters = {'Bucket': 'my-bucket', 'Prefix': 'foo/baz'} page_iterator = paginator.paginate(**operation_parameters) for page in page_iterator: print(page['Contents']) 以使用swashbuckle版本? https://github.com/Microsoft/aspnet-api-versioning

在我的Aspnet API中,我有以下代码来初始化基于属性的路由,api版本控制和swagger。

Startup.cs

当ResolveVersionSupportByRouteConstraint方法触发路由模板时,包括文字api字符串“api / v {version} / users”我的用户控制器用[ApiVersion(“1.0”)]进行修饰,我定义了以下路由[Route(“ API / v {版本:apiVersion} /用户“)]。当我用邮递员点击api / v1 / users时调用有效,但我无法弄清楚如何使用Swashbuckle / Swagger。

我希望我的swagger文档看起来像asp.net核心api样板文件的示例,除了我使用Owin与owin启动类而不是.net core:https://github.com/ASP-NET-Core-Boilerplate/Templates/blob/master/MVC%206%20API.md

2 个答案:

答案 0 :(得分:5)

您可以找到示例here 这就是我在启动自托管owin app时的方法:

public void Configuration(IAppBuilder appBuilder)
    {
        HttpConfiguration config = new HttpConfiguration();
        //configure your app

        config.AddApiVersioning(o =>
        {
            o.ReportApiVersions = true;
            o.ApiVersionReader = new UrlSegmentApiVersionReader();
        });
        var constraintResolver = new DefaultInlineConstraintResolver()
        {
            ConstraintMap = { ["apiVersion"] = typeof(ApiVersionRouteConstraint) }
        };
        config.MapHttpAttributeRoutes(constraintResolver);
        SwaggerConfiguration.Configure(config);            
        appBuilder.UseWebApi(config);
    }

swagger的配置非常简单,主要部分是VersionedApiExplorer(确保你传递了api的右组名格式,我的格式是v1,v2等):

public static class SwaggerConfiguration
{       
    public static void Configure(HttpConfiguration config)
    {
        var apiExplorer = config.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'V");
        config.EnableSwagger(
                swagger =>
                {
                    swagger.MultipleApiVersions(
                        (apiDesc, targetApiVersion) => apiDesc.GetGroupName() == targetApiVersion,
                        versionBuilder =>
                        {
                            foreach (var group in apiExplorer.ApiDescriptions)
                            {
                                var description = "";
                                if (group.IsDeprecated) description += "This API deprecated";

                                versionBuilder.Version(group.Name, $"Service API {group.ApiVersion}")
                                    .Description(description);
                            }
                        });
                    swagger.DocumentFilter<VersionFilter>();
                    swagger.OperationFilter<VersionOperationFilter>();
                })
            .EnableSwaggerUi(cfg => cfg.EnableDiscoveryUrlSelector());
    }

在控制器中添加属性ApiVersion和RoutePrefix

[ApiVersion("1")]
[RoutePrefix("api/v{version:apiVersion}/history")]
public class HistoryController: ApiController

如果您对VersionFilter和VersionOperationFilter感到困惑,那么就有代码。此过滤器在swagger中修改生成的路由和参数(不包括您的路由将看起来像/ v {version} / {actionName}并包含所需的参数版本)

public class VersionFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths = swaggerDoc.paths
            .ToDictionary(
                path => path.Key.Replace("v{version}", swaggerDoc.info.version),
                path => path.Value
            );
    }
}
public class VersionOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var version = operation.parameters?.FirstOrDefault(p => p.name == "version");
        if (version != null)
        {
            operation.parameters.Remove(version);
        }
    }
}

答案 1 :(得分:0)

我认为ResolveVersionSupportByRouteConstraint方法可能有误,请参阅:https://github.com/domaindrivendev/Swashbuckle/issues/197#issuecomment-75288894