AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied

Zeus82 picture Zeus82 · Nov 25, 2016 · Viewed 42.1k times · Source

I am creating a website using ASP.NET Core MVC. When I click on an action I get this error:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

Web.Controllers.ChangeEventsController.Create (Web)
Web.Controllers.ProductsController.CreateChangeEvent (Web)

This is how I defined my action in the index.cshtmlm for my ProductsController:

<a asp-controller="ChangeEvents" asp-action="Create" asp-route-id="@item.Id">Create Change Event</a>

Here is my routing:

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

Here is how I defined the actions:

// ChangeEventsController
[HttpGet("{id}")]
public IActionResult Create(Guid id)

// ProductsController
[HttpGet("{id}")]
public IActionResult CreateChangeEvent(Guid id)

What have I done wrong?

Update

Thanks @MegaTron for your response, however I would like to know why I can't have the same action path for different controllers. I feel like the solution you proposed won't scale well if I have many controllers that each create entities.

Answer

Roman Marusyk picture Roman Marusyk · Nov 25, 2016

Try:

// ChangeEventsController
[HttpGet("Create/{id}")]
public IActionResult Create(Guid id)

// ProductsController
[HttpGet("CreateChangeEvent/{id}")]
public IActionResult CreateChangeEvent(Guid id)