JP Hellemons
JP Hellemons

Reputation: 6057

Multiple controller types were found that match the URL MVC 5.2

I get this error when I hit the url Shop/Checkout

The request has found the following matching controller types:

  • shopmvc.Controllers.HomeController
  • shopmvc.Controllers.ProductsController

My HomeController.cs:

[Route("{action=index}")]
public class HomeController : Controller
{
    [Route("Shop/Checkout")]
    public ActionResult Checkout()
    {
    }
}

My ProductsController.cs:

[RoutePrefix("Shop")]
[Route("{action=index}")]
public class ProductsController : Controller
{
    [HttpGet]
    [Route("{brand}/{category}/{subcategory?}/{page:int?}")]
    public ActionResult Index(string brand, string category, string subcategory, int? page, SortOptions currentSort = SortOptions.SinceDesc)
    {
    }

    [HttpGet]
    [ActionName("Details")]
    [Route("{brand}/{category}/{productid}")]
    public ActionResult Details(int productid)
    {
    }
}

I get it that both routes have Shop in it, but I have no clue how to resolve this. This is the razor code in my shared layout:

<a href="@Url.Action("checkout", "Home" )">

Upvotes: 0

Views: 606

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239460

The issue is that "Checkout" is valid as a parameter for brand in your ProductController routes. There's no intrinsic order to routes with attribute routing, so you have to be more careful to make sure only one route can truly match the URL. In your case here, you can simply do something like:

[Route("{brand:regex((?!Checkout))}/...")]

Upvotes: 1

Related Questions