MVC5,WebAPI2和Ninject参数的构造函数错误 [英] MVC5, WebAPI2 and Ninject Parameterless constructor error
问题描述
所以,我有完全相反的问题,因为 MVC5,网页API 2和Ninject
So I have the exact opposite problem as MVC5, Web API 2 and and Ninject
我有一个新的MVC5 / WebAPI2工程,即同时具有控制器S和ApiControllers。
I have a new MVC5/WebAPI2 project, that has both "Controller"s and "ApiControllers".
我使用Ninject.Web.WebAPI的最新版本不稳定,没有code更改NinjectDependencyResolve.cs和Ninject.WebCommom.cs(除了绑定我的依赖)的ApiController的构造函数注入的作品。然而,当我叫MVC控制器我得到:
I'm using the latest unstable version of Ninject.Web.WebAPI with no code changes to NinjectDependencyResolve.cs and Ninject.WebCommom.cs (besides binding my dependency) the ApiController's constructor injection works. However, when I call a MVC Controller I get:
此对象定义无参数的构造函数。
No parameterless constructor defined for this object.
推荐答案
的问题是你需要依赖解析器为MVC和的WebAPI。根据所设定的Ninject库使用,你只能得到那些你在有线之一。
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.
即。如果使用Ninject.Web.WebAPI库,您将需要手动设置MVC解析器:
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));
(我在NinjectWebCommon.cs这样做 CreateKernel()
)
您Ninject解析器可以继承为的WebAPI和MVC接口:
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
这篇关于MVC5,WebAPI2和Ninject参数的构造函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!