- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何配置 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/
使用属性路由时是否可以在 Swagger UI/Swashbuckle 中利用 MultipleApiVersions? 具体来说,我通过以下方式实现了版本控制: using System.Web.H
如何配置 swashbuckle 以使用 Aspnet API verisoning? https://github.com/Microsoft/aspnet-api-versioning 在我的 S
我是一名优秀的程序员,十分优秀!