web api 多版本控制重要的兩個類
阿新 • • 發佈:2018-12-31
1、版本路徑替換
public class ReplaceVersionWithExactValueInPath : IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context) { swaggerDoc.Paths = swaggerDoc.Paths .ToDictionary( path => path.Key.Replace("v{version}", swaggerDoc.Info.Version), path => path.Value ); } }
2、在訪問時去掉版本引數。自動獲得版本的值傳入。
public class RemoveVersionFromParameter : IOperationFilter { public void Apply(Operation operation, OperationFilterContext context) { if (operation.Parameters.Count > 0) { var versionParameter = operation.Parameters.FirstOrDefault(p => p.Name == "version"); operation.Parameters.Remove(versionParameter); } } }