【问题标题】:Error using Ninject with ASP.NET V4在 ASP.NET V4 中使用 Ninject 时出错
【发布时间】:2014-01-28 14:46:40
【问题描述】:

我正在尝试使用本指南了解 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);
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

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

    使用该文件加载您的模块并配置您的绑定。据我所知,您不再需要使用这个 Global.asax 代码,所以不要在其中子类化 NinjectHttpApplication

    【讨论】:

    • 谢谢,我使用的指南是用于手动下载 DLL 的,因为我使用的是 NuGet,所以比我想象的要容易得多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    相关资源
    最近更新 更多