XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.ServerInteractive

[DLL] Server interaction extension, including user identity verification and querying in conjunction with AutoConfig

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.ServerInteractive

提取路由优先级计算方法至工具类复用

将 GetWildcardPatternPriority 方法从 XFEServerCore.cs 移至 RouteMatchHelper 工具类,作为公共静态方法复用,优化代码结构并提升可维护性。XFEServerCore 中相关调用已同步调整。该方法用于根据字面量段和通配符段数量计算路由模式优先级。

fa181ad
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

2 个文件 +22 -17
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Helpers/RouteMatchHelper.cs +20 -0
@@ -36,4 +36,24 @@ internal static class RouteMatchHelper
36 36
37 37 return !patternSegments.Where((t, i) => t != "*" && t != routeSegments[i]).Any();
38 38 }
39
40 /// <summary>
41 /// 计算路由模式的优先级,优先级规则如下
42 /// </summary>
43 /// <param name="pattern"></param>
44 /// <returns></returns>
45 public static int GetWildcardPatternPriority(string pattern)
46 {
47 var segments = pattern.Split('/', StringSplitOptions.RemoveEmptyEntries);
48 var literalSegmentCount = 0;
49 var wildcardSegmentCount = 0;
50 foreach (var segment in segments)
51 {
52 if (segment == "*")
53 wildcardSegmentCount++;
54 else
55 literalSegmentCount++;
56 }
57 return (literalSegmentCount * 1000) - (wildcardSegmentCount * 10) + pattern.Length;
58 }
39 59 }
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCore.cs +2 -17
@@ -188,27 +188,12 @@ public abstract class XFEServerCore : ServerCoreServiceBase
188 188 }
189 189 else
190 190 {
191 // 尝试通配符匹配:在所有命中的候选中选择最具体的模式(字面量段越多越优先),避免结果依赖注册顺序
192 static int GetWildcardPatternPriority(string pattern)
193 {
194 var segments = pattern.Split('/', StringSplitOptions.RemoveEmptyEntries);
195 var literalSegmentCount = 0;
196 var wildcardSegmentCount = 0;
197 foreach (var segment in segments)
198 {
199 if (segment == "*")
200 wildcardSegmentCount++;
201 else
202 literalSegmentCount++;
203 }
204 return (literalSegmentCount * 1000) - (wildcardSegmentCount * 10) + pattern.Length;
205 }
206
207 191 var bestPriority = int.MinValue;
208 192 foreach (var (pattern, factory) in WildcardCoreServiceList)
209 193 {
210 194 if (!RouteMatchHelper.MatchWildcardRoute(pattern, route)) continue;
211 var currentPriority = GetWildcardPatternPriority(pattern);
195 // 尝试通配符匹配:在所有命中的候选中选择最具体的模式(字面量段越多越优先),避免结果依赖注册顺序
196 var currentPriority = RouteMatchHelper.GetWildcardPatternPriority(pattern);
212 197 if (currentPriority <= bestPriority) continue;
213 198 bestPriority = currentPriority;
214 199 matchedPattern = pattern;