使用Ninject与ASP.NET V4时出错

时间:2014-01-28 14:46:40

标签: c# asp.net asp.net-mvc asp.net-mvc-4 ninject

我正在尝试使用本指南了解ASP.NET MVC4中的依赖注入:

http://www.codeproject.com/Articles/412383/Dependency-Injection-in-asp-net-mvc4-and-webapi-us

但是,当我尝试运行应用程序时,我会抛出以下异常:

An exception of type 'System.IO.FileNotFoundException' occurred in Ninject.dll but was not handled in user code

Additional information: Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

我在Windows 7上使用Visual Studio 2013 Express Edition。下面是我的global.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Cars.Domain.Abstract;
using Cars.Domain.Concrete;
using Ninject;
using Ninject.Web.Common;
using Ninject.Web.Mvc;

namespace Cars.WebUI
{
    public class MvcApplication : NinjectHttpApplication
    {
        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());
            kernel.Bind<IModelRepository>().To<EFModelRepository>();
            return kernel;
        }

        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

Ninject.MVC3 安装Nuget软件包时,它应该在MVC项目的NinjectWebCommon.cs文件夹中创建一个文件App_Start

使用该文件加载模块并配置绑定。据我所知,您不再需要使用此 Global.asax 代码,因此请勿在其中继承NinjectHttpApplication