Error using Ninject with ASP.NET V4

做~自己de王妃 提交于 2019-12-24 12:17:54

问题


I'm trying to learn about Dependency Injection in ASP.NET MVC4 using this guide:

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

However I am getting the following exception being thrown when I try and run the application:

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.

I am using Visual Studio 2013 Express Edition on Windows 7. Below is my 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:


When you install the Nuget package for Ninject.MVC3, it should create a file NinjectWebCommon.cs inside the App_Start folder of your MVC project.

Use that file to load your modules and configure your bindings. As far as I know, you don't need to use this Global.asax code anymore, so don't subclass the NinjectHttpApplication inside it.



来源:https://stackoverflow.com/questions/21408733/error-using-ninject-with-asp-net-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!