Surendra Sharma

Surendra Sharma

Search This Blog

Monday, January 8, 2018

Fix : Duplicate controller Issue in Sitecore Helix project

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:

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)
                new { controller = "Accounts", id = UrlParameter.Optional },
                new[] { "Feature.Abc.Accounts.Controllers" }

                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

        <processor type="MyProject.Website.RegisterCustomRoute, MyProject.Website" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />

If you are facing the same error, make sure to add entry in “RegisterCustomRoute.cs” as highlighted above.

No comments:

Post a Comment