问题描述
我试图用这个指南,了解依赖注入在ASP.NET MVC4:
的
但是我收到以下异常被抛出时,我尝试和运行应用程序:
类型'System.IO.FileNotFoundException'的异常出现在Ninject.dll但在用户code没有处理其他信息:无法加载文件或程序'System.Web.Mvc,版本= 3.0.0.0,文化=中性公钥= 31bf3856ad364e35或它的一个依赖。该系统找不到指定的文件。
我使用Visual Studio 2013前$在Windows 7 p $ PSS版下面是我global.aspx
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Reflection;
使用的System.Web;
使用System.Web.Http;
使用System.Web.Mvc;
使用System.Web.Routing;
使用Cars.Domain.Abstract;
使用Cars.Domain.Concrete;
使用Ninject;
使用Ninject.Web.Common;
使用Ninject.Web.Mvc;命名空间Cars.WebUI
{
公共类MvcApplication:NinjectHttpApplication
{
保护覆盖的iKernel CreateKernel()
{
VAR内核=新StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind&所述; IModelRepository方式>()到< EFModelRepository>();
返回内核;
} 保护覆盖无效OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
当您安装的NuGet包 Ninject.MVC3 ,应该创建一个文件 NinjectWebCommon.cs
你的MVC项目的 App_Start
文件夹中。
使用该文件来加载模块,并配置您的绑定。据我所知,你不需要用这个的的Global.asax 的code了,所以不要子类化 NinjectHttpApplication
里面。
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);
}
}
}
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.
这篇关于使用Ninject与ASP.NET V4错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!