gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:16:23 42 4
gpt4 key购买 nike

如何配置 swashbuckle 以使用 Aspnet API verisoning? https://github.com/Microsoft/aspnet-api-versioning

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

var constraintResolver = new DefaultInlineConstraintResolver()
{
ConstraintMap =
{
["apiVersion"] = typeof( ApiVersionRouteConstraint )
}
};
config.MapHttpAttributeRoutes(constraintResolver);
config.AddApiVersioning();

config.EnableSwagger(c =>
{
c.MultipleApiVersions(
(apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
(vc) =>
{
vc.Version("v1", "Swashbuckle Dummy API V1");
vc.Version("v2", "Swashbuckle Dummy API V2");
});
}



public static bool ResolveVersionSupportByRouteConstraint(ApiDescription apiDesc, string targetApiVersion)
{
var versionConstraint = (apiDesc.Route.Constraints.ContainsKey("apiVersion"))
? apiDesc.Route.Constraints["apiVersion"] as RegexRouteConstraint
: null;

return (versionConstraint == null)
? false
: versionConstraint.Pattern.Split('|').Contains(targetApiVersion);
}

当 ResolveVersionSupportByRouteConstraint 方法触发时,路由模板包含文字 api 字符串“api/v{version}/users” 我的用户 Controller 装饰有 [ApiVersion("1.0")] 并且我定义了以下路由 [Route("api/v{version:apiVersion}/users")]。当我用 postman 点击 api/v1/users 时,调用有效,但我无法弄清楚如何使用 Swashbuckle/Swagger 使其工作。

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

最佳答案

你可以找到例子here这就是我在启动自托管 owin 应用程序时的做法:

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 groupnameformat,我的格式是 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());
}

在 Controller 中添加属性 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);
}
}
}

关于c# - 如何使用 aspnet ApiVersioning 在 Swashbuckle 中配置 MultipleApiVersions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44376295/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com