Monday, February 5, 2018

Sitecore multiple types were found that match the controller

After deploying a set of MVC controllers to my Sitecore 9 instance (with Experience Commerce 9.0), the following error appeared after attempting to call the controller via JavaScript.
Multiple types were found that match the controller named 'Account'. This can happen if the route that services this request ('CommonApi/{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. 
The request for 'Account' has found the following matching controllers:
Sitecore.Commerce.XA.Feature.Account.Controllers.AccountController
MyProject.Foundation.API.Areas.CommonApi.Controllers.AccountController
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Multiple types were found that match the controller named 'Account'.
This was occurring because both my custom code and the Commerce Experience Accelerator had an account controller - even though my controller was contained inside an MVC area. The fix for this, was to reference the namespace when I defined the route.
RouteTable.Routes.MapRoute("Login","CommonApi/{controller}/{action}/{id}", new { area = "CommonApi", controller = "Account", action = "Login", id = UrlParameter.Optional }, new[] { "MyProject.Foundation.API.Areas.CommonApi.Controllers" });

No comments:

Post a Comment