ASP.NET Core 7中重定向至Areas区域Blazor页面时出错
ASP.NET Core 7 端点匹配歧义问题解决
问题说明
在ASP.NET Core 7项目中,通过_Host.cshtml.cs执行代码return RedirectToPage("/Account/Login/Index", new { area = "Identity" });重定向到Identity区域的Blazor页面时,触发AmbiguousMatchException异常,提示请求匹配了多个端点:
An unhandled exception occurred while processing the request. AmbiguousMatchException: The request matched multiple endpoints. Matches: /Account/Login/Index /Account/Login /_Host Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
解决方法
1. 修正重定向路径
Identity默认登录页的路由是/Account/Login(对应Login.cshtml),无需指定Index。修改重定向代码为:
return RedirectToPage("/Account/Login", new { area = "Identity" });
2. 检查路由配置
打开Program.cs,确认Identity相关路由没有重复定义。标准配置如下,若有额外添加的/Account/Login类路由,需移除:
app.MapRazorPages(); app.MapBlazorHub(); app.MapFallbackToPage("/_Host");
3. 明确页面路由模板
检查Login.cshtml和Login/Index.cshtml的@page指令,确保路由模板无重叠。例如:
Login.cshtml的指令应为@page "/Account/Login"Login/Index.cshtml的指令应为@page "/Account/Login/Index"
避免使用模糊的路由模板(如带可选参数的模板)导致匹配歧义。
4. 删除冗余页面
如果Login/Index.cshtml属于冗余页面,或与上级Login.cshtml功能重复,直接删除该页面,从根源消除路由冲突。
内容的提问来源于stack exchange,提问作者khteh
相关产品推荐
相关产品推荐

