This commit is contained in:
余宇波 2025-10-18 18:33:55 +08:00
parent 71f1368e6b
commit c8ad77df21

View File

@ -5,6 +5,7 @@ using MyCode.Project.Services.Implementation;
using MyCode.Project.Services.IServices; using MyCode.Project.Services.IServices;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Http; using System.Web.Http;
namespace MyCode.Project.WebApi.Controllers namespace MyCode.Project.WebApi.Controllers
@ -103,6 +104,34 @@ namespace MyCode.Project.WebApi.Controllers
} }
#endregion #endregion
/// <summary>
/// 替换或添加URL中的端口号支持指定协议
/// </summary>
/// <param name="input"></param>
/// <param name="newPort"></param>
/// <param name="protocol"></param>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public string ReplaceOrAddPortAdvanced(string input, string newPort, string protocol = "http")
{
// 构建协议特定的模式
string protocolPattern = string.IsNullOrEmpty(protocol) ? @"(https?|ftp)://" : $"{protocol}://";
// 匹配已有端口号
string patternWithPort = $@"({protocolPattern}[^/]+?):\d+";
// 匹配没有端口号
string patternWithoutPort = $@"({protocolPattern}[^/:/]+)(?=/|$)";
// 先尝试替换已有端口号
string result = Regex.Replace(input, patternWithPort, $"$1:{newPort}");
// 如果没有端口号被替换,尝试添加端口号
if (result == input)
result = Regex.Replace(input, patternWithoutPort, $"$1:{newPort}");
return result;
}
//#region 订单查询 //#region 订单查询