【问题标题】:ASP.NET Core 3.1 MVC returning AmbiguousMatchException, server error 500ASP.NET Core 3.1 MVC 返回 AmbiguousMatchException,服务器错误 500
【发布时间】:2020-10-20 06:06:42
【问题描述】:

我正在使用 ASP.NET Core 3.1 MVC 和领域。

这个问题发生在使用 IIS Express 的开发中,我什至还没有达到可以部署到实时 (IIS) 服务器的程度。服务器在启动过程结束时抛出错误 500。

例外:

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:请求匹配多个端点。

匹配:

FleetLogix.Intranet.Areas.Maps.Controllers.HomeController.Index (FleetLogix.Intranet)
FleetLogix.Intranet.Areas.Admin.Controllers.HomeController.Index (FleetLogix.Intranet)
FleetLogix.Intranet.Controllers.HomeController.Index (FleetLogix.Intranet)

在 Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] CandidateState)
在 Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] CandidateState)
在 Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] CandidateState)
在 Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)
在 Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.MatchAsync(HttpContext httpContext)
在 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
在 Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext 上下文)
在 Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext 上下文)
在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)

这是我的路线:

app.UseEndpoints(endpoints =>
{
    endpoints.MapAreaControllerRoute(
        name: "AdminArea",
        areaName: "Admin",
        pattern: "Admin/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapAreaControllerRoute(
        name: "AnchorArea",
        areaName: "Anchor",
        pattern: "Anchor/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapAreaControllerRoute(
        name: "DashboardArea",
        areaName: "Dashboard",
        pattern: "Dashboard/{controller=DriverBehaviour}/{action=Index}/{id?}");

    endpoints.MapAreaControllerRoute(
        name: "MapsArea",
        areaName: "Maps",
        pattern: "Maps/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapAreaControllerRoute(
        name: "ReportGroupsArea",
        areaName: "ReportGroups",
        pattern: "ReportGroups/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapAreaControllerRoute(
        name: "MaintenanceArea",
        areaName: "Maintenance",
        pattern: "Maintenance/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapControllers();
    endpoints.MapRazorPages();

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

我没有做任何特别的事情。我已经看到了说要向方法添加属性的修复程序,例如:

[HttpGet("{id:int}")]

但这是在抱怨具有相同配置文件的基本索引方法。通往他们的道路是为了区分。

我该如何解决这个问题?

【问题讨论】:

    标签: asp.net-mvc asp.net-core routes


    【解决方案1】:

    路径“/”或“/Home/Index”请求匹配多个端点。您可以添加区域属性来分隔。所有测试和工作。

    1. [Area("Admin")] 添加到/Area/Admin/HomeController

        [Area("Admin")]
        public class HomeController : Controller
        {
             public IActionResult Index()
             {
                 return View();
             }
        }
      
    2. [Area("Maps")] 添加到/Area/Maps/HomeController

       [Area("Maps")]
       public class HomeController : Controller
       {
           public IActionResult Index()
           {
               return View();
           }
       }
      

    现在你有了这些映射:

    • localhost:44388/admin => /Area/Admin/HomeController/Index Action
    • localhost:44388/maps => /Area/Maps/HomeController/Index Action
    • localhost:44388/ => /HomeController/索引操作

    【讨论】:

    • 远离我的办公桌,但我知道在大多数控制器上都指定了 [Area(xyz)]。我吃完午饭回来看看
    • :) 好的,如果您有任何问题,请随时提问。
    • 大多数确实有该属性,AdminController 没有。已添加它,以及有效的 [Authorize(Policy = "Requirexyz")] 也缺少它。我仍然有问题,但它们是值得提出新问题的不同问题。 TYVM 为您提供帮助。:)
    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 2016-08-06
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多