Could not create controller with Constructor on type not found exception – Sitecore 8.2

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!

Front-End
TypeScript: type aliases to check type equality

This post is to analyse how the type equality check by using type aliases proposed by Matt Pocock in his twitter post. These type aliases allow us to elegantly express type equality checks in unit tests. All we need to do is to pass the output and expected types in …

Front-End
Fixing it.only type error in Jest

If you are getting a type error with it.only in Jest, it could be due to incorrect TypeScript typings or incompatible versions of Jest and TypeScript. To resolve this issue, you can try the following steps: Make sure you have the latest versions of Jest and its TypeScript typings installed …

Front-End
yup conditional validation example

Here’s an example of a Yup validation logic where the first input field is optional but, if filled, it must contain only alphabetic characters, and the second input field is required: import * as Yup from “yup”; const validationSchema = Yup.object().shape({ firstField: Yup.string().matches(/^[A-Za-z]*$/, { message: “First field must contain only …