MVC5, WebAPI2 and Ninject Parameterless constructor error

后端 未结 1 542
北海茫月
北海茫月 2020-12-21 04:51

So I have the exact opposite problem as MVC5, Web API 2 and Ninject

I have a new MVC5/WebAPI2 project, that has both \"Controller\"s and \"ApiControllers\".

相关标签:
1条回答
  • 2020-12-21 05:31

    The issue is you need a Dependency Resolver for both MVC and WebAPI. Depending on which set of Ninject libraries you use, you only get one of those wired in for you.

    i.e. if you use the Ninject.Web.WebAPI library you will need to manually set the MVC resolver:

    System.Web.Mvc.DependencyResolver.SetResolver(new NinjectResolver(kernel)); 
    

    (I did this in NinjectWebCommon.cs CreateKernel())

    Your Ninject resolver can inherit the interface for both WebAPI and MVC:

    public class NinjectResolver : NinjectScope, 
        System.Web.Http.Dependencies.IDependencyResolver, 
        System.Web.Mvc.IDependencyResolver
    
    0 讨论(0)
提交回复
热议问题