Could not create controller with Constructor on type not found exception – Sitecore 8.2
- By : Mydatahack
- Category : Sitecore, Web Technologies
- Tags: Controller Renderings, MVC, Sitecore 8.2, Troubleshoot
When your controller rendering is failing with Could not create controller along with the inner exception,Constructor on type (your constructor name, e.g Sitecore.Feature.Controllers.MediaController) not found, there is something wrong with the actual controller code.
Could not create controller error has a few different reasons. Another common reason is the inner exception, does not implement IController, which is typically caused by the wrong Controller value in the rendering property.
In this case, you probably configured everything correctly. The most likely reason is the way you are doing dependency injection in the controller.
Error message
Error Rendering Controller: SitecoreDev.Feature.Articles.Controllers.ArticlesController, SitecoreDev.Feature.Articles. Action: BlogPost: Could not create controller: 'SitecoreDev.Feature.Articles.Controllers.ArticlesController, SitecoreDev.Feature.Articles'. The item being rendered is: '/sitecore/content/Home/MyBlogPost'. The context item is: '/sitecore/content/Home/MyBlogPost'. The current route url is: '{*pathInfo}'. This is the default Sitecore route which is set up in the 'InitializeRoutes' processor of the 'initialize' pipeline. at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName) at Sitecore.Mvc.Controllers.ControllerRunner.GetController() at Sitecore.Mvc.Controllers.ControllerRunner.Execute() at Sitecore.Mvc.Presentation.ControllerRenderer.Render(TextWriter writer) at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args) Inner Exception: Constructor on type 'SitecoreDev.Feature.Articles.Controllers.ArticlesController' not found. at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) at Sitecore.Mvc.Helpers.TypeHelper.CreateObject[T](Type type, Object[] parameters) at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateControllerInstance(RequestContext requestContext, String controllerName) at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)
Solution
For example, if you are injecting multiple dependencies into the controller, the code below does not work.
public class ArticlesController : SitecoreController { private readonly IContentService _contentService; private readonly ICommentService _commentService; public ArticlesController(IContentService contentService, ICommentService commentService) { _contentService = contentService; _commentService = commentService; } ..... }
Instead, you need to call the overloaded constructor using this key word (here for the official Microsoft doc).
public class ArticlesController : SitecoreController { private readonly IContentService _contentService; private readonly ICommentService _commentService; public ArticlesController() : this(new SitecoreContentService(), new BlogCommentService()) { } public ArticlesController(IContentService contentService, ICommentService commentService) { _contentService = contentService; _commentService = commentService; } ..... }
Fixed!