/// <summary> /// 格式化 url 地址 /// </summary> /// <param name="url"></param> /// <returns></returns> private string FormatUrl(string url) { var configurHandler = new ConfigurHandler(); foreach (Match urlKey in new Regex(@"(?<={)\w+(?=})").Matches(url)) { var v = urlKey.Value; var serviceUrl = configurHandler.GetAppOptionValue(v, ""); var replace = url.Replace("{" + v + "}", serviceUrl); url = replace; } return(url); }
/// <summary> /// 获取 请求 url /// </summary> /// <param name="url">url . </param> /// <returns> 返回url . </returns> private string GetUrl(string url) { var httpType = new ConfigurHandler().GetAppOptionValue("HttpType", @default: "http"); if (httpType.ToLower() != "http" && httpType.ToLower() != "https") { httpType = "http"; } if (url.IndexOf("http", StringComparison.Ordinal) == 0) { // 如果是 http 开头, 则说明是完整的 url 地址 直接返回即可 return(url); } if (url.IndexOf("api", StringComparison.Ordinal) == 0 || url.IndexOf("/api", StringComparison.Ordinal) == 0) { if (url.IndexOf('/') != 0) { url = $"/{url}"; } // 如果 地址以 api 或者 /api 开头说明是需要从配置文件中获取 请求ip的url var valueKey = new Regex(@"(?<=api/)\w+").Match(url).Value; var serviceUrl = new ConfigurHandler().GetAppOptionValue <string>("ServiceUrl", valueKey); if (serviceUrl.IsNullOrEmpty()) { return($"{httpType}://{valueKey}-service{url}"); } if (serviceUrl == "{https}") { return($"https://{valueKey}-service{url}"); } if (serviceUrl == "{http}") { return($"http://{valueKey}-service{url}"); } return(serviceUrl + url); } return(FormatUrl(url)); }