简体   繁体   中英

“AmbiguousMatchException: The request matched multiple endpoints” error in ASP.Net Core and ExtCore.Mvc library

I'm using ExtCore.Mvc library and there's a problem when I have 2 controllers with the same name in 2 different extensions. Here's my code:

In ExtensionA:

public class UseEndpointsAction : IUseEndpointsAction
{
    public int Priority => 1000;

    public void Execute(IEndpointRouteBuilder endpointRouteBuilder, IServiceProvider serviceProvider)
    {
        endpointRouteBuilder.MapControllerRoute(
            name: "ExtensionA",
            pattern: "ExtensionA",
            defaults: new { controller = "Default", action = "Index" });
    }
}

In ExtensionB:

public class UseEndpointsAction : IUseEndpointsAction
{
    public int Priority => 1000;

    public void Execute(IEndpointRouteBuilder endpointRouteBuilder, IServiceProvider serviceProvider)
    {
        endpointRouteBuilder.MapControllerRoute(
            name: "ExtensionB",
            pattern: "ExtensionB",
            defaults: new { controller = "Default", action = "Index" });
    }
}

There are two controllers as "ExtensionA.Controllers.DefaultController" and "ExtensionB.Controllers.DefaultController".

When client requests this URL: "http://{domain}/ExtensionA", it gets the following error:

An unhandled exception occurred while processing the request. AmbiguousMatchException: The request matched multiple endpoints. Matches: ExtensionA.Controllers.DefaultController.Index (ExtensionA) ExtensionB.Controllers.DefaultController.Index (ExtensionB)

I want to solve it using namespace which means to resolve each route to a specific namespace. I search through the web and I read all MSDN documents yet found nothing. Any help would be appreciated.

Thanks

Rename your controllers (and change controller = "Default" ) to DefaultControllerA and DefaultControllerB respectively.

Or you can use areas .

Or try implementing some kind of NamespaceConstraintAttribute like was done here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM