UPDATE - Please look at my answer for a link and explanation of the solution to this problem
Before we start, I know this is a very common quest
The whole thing is just ridiculous, I switched to Autofac, tired of never one time being able to add Ninject to a project successfully. Web API, MVC4, MVC5 all had problems.
For MVC5 people, using Ninject MVC3, add the following to web.config app level:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
I fixed mine by Removing Ninject MVC5 and installing Ninject MVC3, from Nuget.
MVC5 == Ninject.MVC5
MVC4 == Ninject.MVC3
MVC3 == Ninject.MVC3
Check the version of the System.Web.MVC in the references to be sure which version of MVC you are currently on.
Well, I don't have an exact answer why the error is coming up, but I do know who is causing it and that is Visual Studio 2012. I installed Visual Studio 2010 on the same machine as 2012, installed ASP.NET MVC 4 for 2010 and I recreated the 2012 project into 2010 word for word, letter for letter. The final result is that when 2010 debugs the project everything works fine and Ninject injects the dependencies as it should.
When 2012 debugs its project it just comes up with the No parameterless constructor defined for this object
exception. Re-targeting between .NET 4.0 and .NET 4.5 in 2012 doesn't do anything. Re-installing Ninject from NuGet also doesn't do anything. I even configured both 2010 and 2012 projects to use the local IIS server to be absolutely sure and the end result is the same.
I'm going to assume that there's a bug with Visual Studio 2012 or with Ninject. The only difference I've got between the two projects is which IDE they're running from and the 2012 project is the one that's crashing so that's why I'm pointing the finger at Visual Studio 2012.
UPDATE
Guys. GUYS! I ran into this problem AGAIN, and found the solution in another SO question: Ninject + MVC3 is not injecting into controller.
Basically, this is what's missing from the Web.config which makes it work:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
I'm guessing this forces the framework to be aware of IoC containers which allows Ninject the finally be able to bind. Although, I can't help but think that the Ninject NuGet package should look for the existence of that binding redirect in the Web.config and auto-magically add it. It sure would help with a lot of hair pulling happening over this issue.
P.S. Up-vote the snot out of that post I linked because it deserves it!
Don't reinvent the wheel and just try Install-Package Ninject.MVC3
I know this is an old question but there don't seem to be any real answers and I've worked around the problem so here is my solution:
Create a custom controller factory:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory(IKernel kernel)
{
ninjectKernel = kernel;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return (controllerType == null) ? null : (IController) ninjectKernel.Get(controllerType);
}
}
Then, if you are using NinjectHttpApplication, add the following line to OnApplicationStarted:
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(Kernel));
If you aren't using NinjectHttpApplication, then add that line somewhere after you have created your kernel and pass it a reference to your freshly created kernel.
That's it.
As all of the previous posters have stated this caused me some gray hairs. In my case I had "optimised" my references too much. Ninject.* stuff was there but had gotten mangled up. I deleted all the references and cleaned packages.conf manualli and added the packages again - victory!