private IEnumerable <ApiDescription> GetApiDescriptionsFor(string apiVersion) { return((_options.VersionSupportResolver == null) ? _apiExplorer.ApiDescriptions : _apiExplorer.ApiDescriptions.Where(apiDesc => _options.VersionSupportResolver(apiDesc, apiVersion))); }
private IEnumerable <ApiDescription> GetApiDescriptionsFor(string apiVersion) { if (okApiDesc.Count == 0) { lock (lockObj) { if (okApiDesc.Count == 0) { Regex regex = new Regex(@"(v\d+)/(\w+)(v\d+)/", RegexOptions.IgnoreCase); //匹配带有版本号的 Regex ctrl = new Regex(@"api/(\w+)(v\d+)/", RegexOptions.IgnoreCase); //匹配没有版本号的 Regex version = new Regex(@"\.(v\d+)", RegexOptions.IgnoreCase); // 处理成有效的 ApiDescription _apiExplorer.ApiDescriptions.Where(a => { var path = a.RelativePath; var cDescriptor = a.ActionDescriptor.ControllerDescriptor; var @namespace = cDescriptor.ControllerType.Namespace; Match match = regex.Match(path); if (match.Success) //匹配成功,path里有版本号 { var group1 = match.Groups[1].Value; //跟在/api 后面的版本号 var group2 = match.Groups[2].Value; //控制器名(大写) 没有匹配上则为空字符串 var group3 = match.Groups[3].Value; //版本号(大写) 没有匹配上则为空字符串 if (!group1.Equals(group3, StringComparison.OrdinalIgnoreCase)) { return(false); } if (@namespace.EndsWith("." + group3, StringComparison.OrdinalIgnoreCase)) { a.RelativePath = a.RelativePath.Replace($"/{group2 + group3}/", $"/{group2[0] + group2.Substring(1).ToLower()}/"); return(true); } else if (group3 == "V1" && version.IsMatch(@namespace) == false) { //命名空间没有版本号,如果path中版本号为v1也合理,默认v1 a.RelativePath = a.RelativePath.Replace($"/{group2 + group3}/", $"/{group2[0] + group2.Substring(1).ToLower()}/"); return(true); } return(false); } else { var ctrlMatch = ctrl.Match(path); if (ctrlMatch.Success) { if (version.IsMatch(@namespace) == false) { var cName = ctrlMatch.Groups[1].Value; //控制器名(大写) var v = ctrlMatch.Groups[2].Value; //版本号(大写) a.RelativePath = a.RelativePath.Replace($"/{cName + v}/", $"/{cName[0] + cName.Substring(1).ToLower()}/"); return(true); } } } return(false); }) .ToList() .ForEach(a => okApiDesc.Add(a)); } } } //根据版本号筛选 ApiDescription return(okApiDesc.Where(apiDesc => _options.VersionSupportResolver(apiDesc, apiVersion)).ToList()); }