Aspnetcore: AmbiguousMatchException using MapAreaRoute with ASP.NET Core 2.2

Created on 21 Feb 2019  路  6Comments  路  Source: dotnet/aspnetcore

Using the following routes:

app.UseMvc(routes =>
{
    routes.MapAreaRoute(
        name: "MyAreaProducts",
        areaName:"Products",
        template: "Products/{controller=Home}/{action=Index}/{id?}"
        );
    routes.MapAreaRoute(
        name: "MyAreaServices",
        areaName: "Services",
        template: "Services/{controller=Home}/{action=Index}/{id?}"
        );
    routes.MapRoute(
       name: "default",
       template: "{controller=Home}/{action=Index}/{id?}");
});

Navigation to https://localhost:5001/Products/Home/About throws:

An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:

MVCareas.Areas.Products.Controllers.HomeController.About (MVCareas)
MVCareas.Controllers.HomeController.About (MVCareas)
Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateSet candidates)

Repo here: Run app and select the Products/Home/About link.

Work around is to disable endpoint routing:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options =>
           options.EnableEndpointRouting = false)
           .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

Using .NET Core 2.2.2

dotnet --version
2.2.104

cc @JamesNK

area-mvc

Most helpful comment

You need to add area descriptor to controller to make it run. Ex.[Area("Admin")]

All 6 comments

Thanks for logging this.

The issues is already fixed in 3.0. To be decided whether to patch in 2.2. Most likely not because there is a simple work-around in 2.2.

@JamesNK, @mkArtakMSFT did this fix made it to .net core 2.2.3, which has just been released?

It didn't. For 2.2 you should disable endpoint routing. In 3.0 this is fixed.

You need to add area descriptor to controller to make it run. Ex.[Area("Admin")]

I had this problem in Core 3.0. I finally found the solution was to put a route attribute on the action - e.g. [Route("NodeInfo")]. That fixed it

Yeap. I just had same problem.
Solution is Route Attribute
Example ;

before class

[Route("areaName/[controller]")]
public class MyController:ControllerBase{}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

FourLeafClover picture FourLeafClover  路  3Comments

mj1856 picture mj1856  路  3Comments

guardrex picture guardrex  路  3Comments

rbanks54 picture rbanks54  路  3Comments

BrennanConroy picture BrennanConroy  路  3Comments