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

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.

Published on Apr 8, 2019

Tags: ASP.NET MVC and Web API Tutorial

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.