我想设置而用于第一次。我有一个IRepository接口,和一个储存库的实施。我在使用ASP.NET 视,并且我想注入实施,像这样:

public class HomeController : Controller
{
    [Inject] public IRepository<BlogPost> _repo { get; set; }

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        var b = new BlogPost
                    {
                        Title = "My First Blog Post!",
                        PostedDate = DateTime.Now,
                        Content = "Some text"
                    };

        _repo.Insert(b);

        return View();
    }

    // ... etc
}

这里是全球性的。asax:

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected override void OnApplicationStarted()
    {
        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel(new BaseModule());
        return (kernel);
    }
}

和这里的BaseModule类:

   public class BaseModule : StandardModule
    {
        public override void Load()
        {
            Bind<IRepository<BlogPost>>().To<Repository<BlogPost>>();
        }
    }

当我浏览的索引()行动,不过,我得到的"对象,参考不设定对象的一个实例"时,尝试使用_repo.插入(b)。我是什么走出去?

有帮助吗?

解决方案

而1.0没有视支持。有各种方法的使用视与而1.0分散各地的网络。

我会推荐获得最新代码,从而干线,其中包括视支持。然后使用下列作为一个起点,应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DemoApp.Models;
using Ninject.Core;
using Ninject.Framework.Mvc;

namespace DemoApp
{
    public class MvcApplication : NinjectHttpApplication
    {
        protected override void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }

        protected override IKernel CreateKernel()
        {
            return new StandardKernel(new BaseModule(), new AutoControllerModule(Assembly.GetExecutingAssembly()));
        }
    }
}

有几件事,突出与你原来的执行情况...

  • 而已 两个 实现的名字 NinjectHttpApplication-一个在 而.框架。网络和一个在 而.框架。视.你会出现 可使用以前作为以后 包含一个保护RegisterRoutes() 法。
  • 你需要的一种方法,而是一个钩入控制器的设立,这是使用ControllerBuilder.该而.框架。视.NinjectHttpApplication注册NinjectControllerFactory.你就必须提供它自己使用而为1.0。
  • 你需要寄你的控制器具的容器。你可以手动,但是使用最新的法规和AutoControllerModule,自动寄存器控制你!

其他提示

你需要添加的 AutoControllerModule 清单的模块指定当创建的核心,显示如下:

protected override IKernel CreateKernel()
{
    IKernel kernel = new StandardKernel(
                         new BaseModule(), 
                         new AutoControllerModule(Assembly.GetExecutingAssembly())
                     );
    return (kernel);
}

AutoControllerModule 是的一部分,视支持而1.x.扫描会提供其构造为视器控制的课程和自动结合。在代码,你必须正确定你的储存库,而而不是负责的活你的控制器。为了你的仓库被注入的一个实例 HomeController 类,而是需要在负责创造和激活的控制器。没有的 AutoControllerModule,视仍然在负责建立控制器;因此,而从来没有得到一个机会,注入任何成员。一次而是负责创造和激活的控制器,注射会发生的预期。

想想 AutoControllerModule 为寻找所有的控制和产生这样的代码(HomeController用作示例):

Bind<HomeController>.ToSelf();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top