If
you are working in Sitecore Helix Framework and going to call action method
like http://myinstance/api/sitecore/accounts/RenderResetPassword,
you may get error like
Server
Error in '/' Application.
Multiple
types were found that match the controller named 'accounts'. This can happen if
the route that services this request ('api/sitecore/{controller}/{action}')
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 'accounts' has found the following matching controllers:
Feature.Abc.Accounts.Controllers.AccountsController
Feature.Xyz.Accounts.Controllers.AccountsController
The request for 'accounts' has found the following matching controllers:
Feature.Abc.Accounts.Controllers.AccountsController
Feature.Xyz.Accounts.Controllers.AccountsController
It’s
because of duplicate controller name in Abc and Xyz project.
To
fix this, we have to register custom MVC routes in Sitecore. For this, I
created one custom processor file “RegisterCustomRoute.cs” to
initialize pipeline.
You
have to register your duplicate controller as
public class RegisterCustomRoute
{
public virtual void Process(PipelineArgs args)
{
RouteTable.Routes.MapRoute
("AbcAccount",
"abc/{controller}/{action}/{id}",
new { controller = "Accounts", id = UrlParameter.Optional },
new[] { "Feature.Abc.Accounts.Controllers" }
);
RouteTable.Routes.MapRoute
("XyzAccount",
"xyz/{controller}/{action}/{id}",
new { controller = "Accounts", id = UrlParameter.Optional },
new[] { "Feature.Xyz.Accounts.Controllers" }
);
}
}
Here
I have append “abc” and “xyz” to separate the URL for account and specified
their Namespace as last parameter in MapRoute() method. Now you can call new URL as
With
this you don’t need to put “api/sitecore” in URL now for
accounts controller.
I
registered this Processor entry in config patch file customweb.config
<pipelines>
<initialize>
<processor type="MyProject.Website.RegisterCustomRoute,
MyProject.Website" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes,
Sitecore.Mvc']" />
</initialize>
</pipelines>
If
you are facing the same error, make sure to add entry in “RegisterCustomRoute.cs”
as highlighted
above.