Hecatonchires
Hecatonchires

Reputation: 1071

ASP.NET Core 3.1 MVC returning AmbiguousMatchException, server error 500

I'm using ASP.NET Core 3.1 MVC and areas.

This problem occurs in development using IIS Express, I haven't even got it to the point I can deploy to a live (IIS) server yet. The server is throwing an error 500 at the end of the startup process.

Exception:

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.

Matches:

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)

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

This is my routing:

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?}");
});

I'm not doing anything special. I've seen fixes that say to add an attribute to the methods eg:

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

but this is complaining about base index methods that have the same profile. The path to them is what's meant to differentiate.

How do I fix this?

Upvotes: 2

Views: 2666

Answers (1)

Michael Wang
Michael Wang

Reputation: 4022

The path "/" or "/Home/Index" request matched multiple endpoints. You could add area attribute to separate. All tested and worked.

  1. Add [Area("Admin")] to /Area/Admin/HomeController:

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

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

Now you have these mappings:

  • localhost:44388/admin => /Area/Admin/HomeController/Index Action
  • localhost:44388/maps => /Area/Maps/HomeController/Index Action
  • localhost:44388/ => /HomeController/Index Action

Upvotes: 4

Related Questions