简体   繁体   English

Ninject和asp.net MVC4

[英]Ninject and asp.net MVC4

I have a MVC3 application that I would like to port to MVC4. 我有一个MVC3应用程序,我想移植到MVC4。 I am using Ninject for dependency injection. 我正在使用Ninject进行依赖注入。 Using Nuget, I added "Ninject" to my project and created a controller factory as shown below 使用Nuget,我在项目中添加了“Ninject”并创建了一个控制器工厂,如下所示

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }

    protected override IController GetControllerInstance(RequestContext  requestContext, Type controllerType)
    {
        return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
    }

    private void AddBindings()
    {
        //Add ninject bindings here
    }
}

This works fine for MVC3 but things have changed in MVC4. 这适用于MVC3,但MVC4中的情况发生了变化。 I did some digging and found this link that explains how to get ninject working for MVC4 我做了一些挖掘,发现这个链接解释了如何让ninject为MVC4工作

http://haacked.com/archive/2012/03/11/itrsquos-the-little-things-about-asp-net-mvc-4.aspx http://haacked.com/archive/2012/03/11/itrsquos-the-little-things-about-asp-net-mvc-4.aspx

However, I am having trouble getting the code in the above link to compile. 但是,我无法在上面的链接中获取代码进行编译。 Specifically, The code that I am supposed to place in the Start() method of the web.common file gives me unresolved namespace errors 具体来说,我应该放在web.common文件的Start()方法中的代码给出了未解决的命名空间错误

GlobalConfiguration.Configuration.ServiceResolver
  .SetResolver(DependencyResolver.Current.ToServiceResolver());

Both "ServiceResolver" and ".SetResolver" are unresolved. “ServiceResolver”和“.SetResolver”都未解析。 What references do I need to add to enable these? 我需要添加哪些参考才能启用这些参考? Also, if possible can you point me towards a tutorial showing me how to get ninject working in MVC4 without having to install the nuget package ninject.mvc3? 另外,如果可能的话,你可以指点我一个教程,向我展示如何让ninject在MVC4中工作,而无需安装nuget包ninject.mvc3吗? I ask because I would prefer not to have any packages installed in my application that were written for MVC3 specifically to avoid things from breaking down the line if these nuget packages are updated. 我问,因为我不希望在我的应用程序中安装任何专门为MVC3编写的软件包,以避免在更新这些nuget软件包时出现故障。

edit: I should have added that I am using Visual studio 2012 and .Net 4.5 编辑:我应该补充一点,我使用的是Visual Studio 2012和.Net 4.5

Phil's article is about using Ninject with WebAPI. Phil的文章是关于将Ninject与WebAPI一起使用。 Is that what you mean? 你是这个意思吗? You don't need to do that for normal MVC injection. 对于正常的MVC注入,您不需要这样做。 Also, you should be using the Ninject.MVC3 NuGet package with MVC4 (yes, I know it says MVC3, but it still works fine because NuGet sets up Assembly Version Redirects that make everything Just Work), this sets everything up and you don't need a controller factory. 此外,你应该使用Ninject.MVC3 NuGet包与MVC4(是的,我知道它说MVC3,但它仍然可以工作,因为NuGet设置汇编版本重定向,使一切正常工作),这设置一切,你不要需要一个控制器工厂。 Just edit the NinjectWebCommon.cs file to add your bindings. 只需编辑NinjectWebCommon.cs文件即可添加绑定。

Just simply Add namespace 只需添加命名空间

Using Ninject;

after installing template from Nuget... U just install "Ninject" but not "Ninject for MVC3" from Nuget Template 从Nuget安装模板后...只需从Nuget模板安装“Ninject”而不是“Ninject for MVC3”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM