如何将 Ninject 与 ASP.NET Web API 一起使用? [英] How to use Ninject with ASP.NET Web API?

查看:32
本文介绍了如何将 Ninject 与 ASP.NET Web API 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MVC 中,我只是创建实现 DefaultControllerFactory 接口的类 NinjectControllerFactory 然后在其中进行一些绑定.最后在 Global 我运行它:

In MVC I simply make the class NinjectControllerFactory that implements DefaultControllerFactory interface then do some bindings in it. at last in Global I run it:

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

但是在 ASP.NET Web API 中使用 Ninject 怎么样?网络上有一些信息,但已过时且为预发布版本.

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.

有没有直接的方法解决这个问题?

推荐答案

很多文章过时的原因是因为自 2012 年 6 月(RC 于 5 月 31 日发布)以来,方法没有改变.您需要添加 Ninject MVC3 Nuget 包,然后实现IDepenencyResolver",然后注册您的实现.

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.

最好的两个演练是:

  1. http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
  2. http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

这篇关于如何将 Ninject 与 ASP.NET Web API 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆