Helping you solve those tough coding problems!

Controller does not have a default constructor Controller does not have a default constructor

Published on Apr 8, 2019 by Jamie Munro

I use Ninjection for my C# MVC project. As I've discussed in Advanced Automatic Ninject Bindings Advanced my Ninjection is automated based on Interfaces matching Class Names. So when the dreaded Controller does not have a default constructor error occurs I have two common starting points.




Ahhh my Ninjection is broken


I do not receive the Controller does not have a default constructor error a lot. It's one of those things that happens once every six months - and of course I never remember why.

The first spot that I always check is that I've setup my automated ninjection properly. In short, does my class match my interface.

For example:

Class name: MeClass : IMyClass.

In this example I have a typo in my class name. I called it MeClass instead of MyClass. This will cause the no default constructor error.

The Dreaded Circular Reference


The next common scenario is that you've added a new class to the controller or child class that causes a circular reference. This will also cause the no default constructor error.

This one of course is so much harder to diagnose. An example of a circular reference would be:

Service 1 uses Service 2
Service 2 uses Service 3
Service 3 uses Service 1

When I'm sure I don't have a typo in my class name, I immediately create a relationship diagram to see if my classes contain a circular reference.

Unfortunately this last one does not have a concrete "here is how to fix it", but hopefully it will start you on the right path.

Tags: ASP.NET | mvc

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP