asp.net-mvc 如何将 Ninject 与 ASP.NET Web API 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14016727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Ninject with ASP.NET Web API?
提问by Blazi
In MVC I simply make the class NinjectControllerFactorythat implements DefaultControllerFactoryinterface then do some bindings in it. at last in GlobalI run it:
在 MVC 中,我只是创建NinjectControllerFactory实现DefaultControllerFactory接口的类,然后在其中进行一些绑定。最后在Global我运行它:
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
But what about using Ninject in ASP.NET Web API? there are some information on the web but are out dated and for pre-released versions.
但是在 ASP.NET Web API 中使用 Ninject 怎么样?网络上有一些信息,但已过时且为预发布版本。
Is there a straightforward way for this problem?
这个问题有直接的方法吗?
采纳答案by Mark Jones
The reason a lot of the articles are old is because the approach hasn't changed since June 2012 (RC released May 31st). You need to add the Ninject MVC3 Nuget package, then implement 'IDepenencyResolver' and then register your implementation.
许多文章过时的原因是因为自 2012 年 6 月(RC 于 5 月 31 日发布)以来,方法没有改变。您需要添加 Ninject MVC3 Nuget 包,然后实现“IDEpenencyResolver”,然后注册您的实现。
The best two walk-thoughs are:
最好的两个步行是:
回答by Chris Pratt
The current version of Ninject.Web.WebApi, since at least 3.2.1.0, no longer requires anything additional to be added manually. Just add the package and register everything in NinjectWebCommon.cs, as usual.
Ninject.Web.WebApi 的当前版本,至少从 3.2.1.0 开始,不再需要手动添加任何其他内容。只需像往常一样添加包并在 NinjectWebCommon.cs 中注册所有内容。
回答by Bryan
For Web Api 2 you need these nuget packages -
对于 Web Api 2,您需要这些 nuget 包 -
Ninject
Ninject.Web.Common
Ninject.Web.WebApi
Ninject.Web.Common.WebHost
WebActivatorEx
And you need to edit NinjectWebCommon.CreateKernel(..)to include
你需要编辑NinjectWebCommon.CreateKernel(..)以包括
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;
I've written a detailed blog post here - http://NoDogmaBlog.bryanhogan.net/2016/04/web-api-2-and-ninject-how-to-make-them-work-together/including a full solution to download.
我在这里写了一篇详细的博客文章 - http://NoDogmaBlog.bryanhogan.net/2016/04/web-api-2-and-ninject-how-to-make-them-work-together/包括完整的解决方案去下载。
回答by D.B
The following steps work like a sharm to get Ninject working on an WebAPI project:
以下步骤就像是让 Ninject 在 WebAPI 项目上工作一样:
- Install the Ninject.Web.WebApi NuGet package.
- Install the Ninject.Web.WebApi.WebHost NuGet package.
- Register your dependencies in the method "RegisterServices" in the file NinjectWebCommon added to the App_Start folder. Like this:
- 安装 Ninject.Web.WebApi NuGet 包。
- 安装 Ninject.Web.WebApi.WebHost NuGet 包。
- 在添加到 App_Start 文件夹的 NinjectWebCommon 文件中的方法“RegisterServices”中注册您的依赖项。像这样:
Private static void RegisterServices(IKernel kernel){
Private static void RegisterServices(IKernel kernel){
kernel.Bind <IFoo>().To <Foo>();
}

