Skip to content

Commit daef0a7

Browse files
committed
fix(http): 修复 HTTP 路由重复注册问题
添加 registeredRouteKeys 集合跟踪已注册路由, 使用 TryAddRouteKey 方法避免因大小写差异导致的重复注册。
1 parent d255354 commit daef0a7

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

GameFrameX.StartUp/AppStartUpByHTTPServer.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private async Task StartHttpServer(List<BaseHttpHandler> baseHandler, Func<strin
195195
}
196196
}
197197

198-
// 注册HTTP处理器路由(同时支持驼峰和下划线格式)
198+
var registeredRouteKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
199199
foreach (var handler in baseHandler)
200200
{
201201
var handlerType = handler.GetType();
@@ -206,12 +206,12 @@ private async Task StartHttpServer(List<BaseHttpHandler> baseHandler, Func<strin
206206
}
207207

208208
// 注册驼峰格式路由
209-
RegisterHandlerRoute(app, mappingAttribute, mappingAttribute.OriginalCmd, httpFactory, aopHandlerTypes, development);
209+
RegisterHandlerRoute(app, mappingAttribute, mappingAttribute.OriginalCmd, httpFactory, aopHandlerTypes, development, registeredRouteKeys);
210210

211211
// 注册下划线格式路由(如果与驼峰格式不同)
212212
if (mappingAttribute.OriginalCmd != mappingAttribute.StandardCmd)
213213
{
214-
RegisterHandlerRoute(app, mappingAttribute, mappingAttribute.StandardCmd, httpFactory, aopHandlerTypes, development);
214+
RegisterHandlerRoute(app, mappingAttribute, mappingAttribute.StandardCmd, httpFactory, aopHandlerTypes, development, registeredRouteKeys);
215215
}
216216
}
217217

@@ -336,11 +336,14 @@ private static string GetHttpMethodDescription(HttpMethodType httpMethod)
336336
/// <param name="httpFactory">HTTP 处理器工厂 / HTTP handler factory</param>
337337
/// <param name="aopHandlerTypes">AOP 处理器列表 / AOP handler list</param>
338338
/// <param name="development">是否为开发环境 / Whether it is development environment</param>
339-
private static void RegisterHandlerRoute(WebApplication app, HttpMessageMappingAttribute mappingAttribute, string cmd, Func<string, BaseHttpHandler> httpFactory, List<IHttpAopHandler> aopHandlerTypes, bool development)
339+
private static void RegisterHandlerRoute(WebApplication app, HttpMessageMappingAttribute mappingAttribute, string cmd, Func<string, BaseHttpHandler> httpFactory, List<IHttpAopHandler> aopHandlerTypes, bool development, ISet<string> registeredRouteKeys)
340340
{
341-
// 注册路由 - 根据 HttpMethod 选择不同的 Map 方法
342341
var apiPath = $"{GlobalSettings.CurrentSetting.HttpUrl}{cmd}";
343342
var httpMethod = mappingAttribute.HttpMethod;
343+
if (!TryAddRouteKey(httpMethod, apiPath, registeredRouteKeys))
344+
{
345+
return;
346+
}
344347

345348
IEndpointConventionBuilder route;
346349
switch (httpMethod)
@@ -370,4 +373,17 @@ private static void RegisterHandlerRoute(WebApplication app, HttpMessageMappingA
370373
});
371374
}
372375
}
373-
}
376+
377+
/// <summary>
378+
/// 尝试添加路由唯一键,避免大小写差异导致重复注册。
379+
/// </summary>
380+
/// <param name="httpMethod">HTTP方法</param>
381+
/// <param name="apiPath">API路径</param>
382+
/// <param name="registeredRouteKeys">已注册路由键集合</param>
383+
/// <returns>添加成功返回 true,已存在返回 false</returns>
384+
private static bool TryAddRouteKey(HttpMethodType httpMethod, string apiPath, ISet<string> registeredRouteKeys)
385+
{
386+
var routeKey = $"{httpMethod}:{apiPath}";
387+
return registeredRouteKeys.Add(routeKey);
388+
}
389+
}

0 commit comments

Comments
 (0)