IApiExplorer apiExplorer){ foreach (PathItem path in swagge

时间:2021-10-09 07:50:59

一、为什么要使用版本控制

(1)项目不竭迭代,在增加新接口时不粉碎原有接口,以保证webapi推出新成果后旧的移动客户端可顺利使用老接口

(2)限制移动端对接口的访谒,为客户端供给特别的成果。

二、webapi版本控制要领

(1)在 URL 中追加版本或作为盘问字符串参数

(2)通过自界说标头和通过接受标头

三、webapi版本控制实现

微软有开源的组件库,不用反复造*,,

四、swagger检察各个版本的webapi

1.如何使用swagger来检察webapi文档,请参阅我的另一篇文章

swagger集成

2.如何检察差别版本有哪些接口,效果如下

IApiExplorer apiExplorer){ foreach (PathItem path in swagge

IApiExplorer apiExplorer){ foreach (PathItem path in swagge

我的版本控制要领基于Header实现,使用的是asp.net mvc,主要的代码已加粗

Startup.cs

public class Startup { public static string VersionName = "api-version"; /// <summary> /// Configures the application using the provided builder. /// </summary> /// <param>The current application builder.</param> public void Configuration( IAppBuilder builder ) { // we only need to change the default constraint resolver for services that want urls with versioning like: ~/v{version}/{controller} var constraintResolver = new DefaultInlineConstraintResolver() { ConstraintMap = { ["apiVersion"] = typeof( ApiVersionRouteConstraint ) } }; var configuration = new HttpConfiguration(); var httpServer = new HttpServer( configuration ); // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions" configuration.AddApiVersioning( o => { o.ReportApiVersions = true; //给与header方法控制版本 o.ApiVersionReader = new HeaderApiVersionReader(Startup.VersionName); } ); configuration.MapHttpAttributeRoutes( constraintResolver ); //集成swagger var apiExplorer = configuration.AddVersionedApiExplorer( o => o.GroupNameFormat = "‘v‘VVV" ); configuration.EnableSwagger( "{apiVersion}/swagger", swagger => { //多版本选择配置 swagger.MultipleApiVersions( ( apiDescription, version ) => apiDescription.GetGroupName() == version, info => { foreach ( var group in apiExplorer.ApiDescriptions ) { var description = "A sample application with Swagger, Swashbuckle, and API versioning."; if ( group.IsDeprecated ) { description += " This API version has been deprecated."; } info.Version(group.Name, $"webapi文档 {group.ApiVersion}"); } } ); //调试时添加版本参数 swagger.DocumentFilter<WordStrQueryVersionFilter>(); //为版本参数设置默认值 swagger.OperationFilter<SwaggerDefaultValuesFilter>(); //载入xml string xmlFile = XmlCommentsFilePath; swagger.IncludeXmlComments(xmlFile); } ) .EnableSwaggerUi( swagger => { swagger.EnableDiscoveryUrlSelector(); }); builder.UseWebApi( httpServer ); } static string XmlCommentsFilePath { get { var basePath = System.AppDomain.CurrentDomain.RelativeSearchPath; var fileName = typeof( Startup ).GetTypeInfo().Assembly.GetName().Name + ".xml"; return Path.Combine( basePath, fileName ); } }

WordStrQueryVersionFilter.cs

/// <summary> /// 将query中的版本号参数改换为head请求方法 /// </summary> public class WordStrQueryVersionFilter : IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) { foreach (PathItem path in swaggerDoc.paths.Values) { var versionParam = path.get.parameters.FirstOrDefault(i => i.name == Startup.VersionName); if (versionParam != null) { var index = path.get.parameters.IndexOf(versionParam); path.get.parameters[index][email protected] = "header"; } } } }

SwaggerDefaultValuesFilter.cs