Ninject 与 ASP.Net webforms 和 MVC [英] Ninject with ASP.Net webforms and MVC

查看:31
本文介绍了Ninject 与 ASP.Net webforms 和 MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个结合了 ASP.Net webforms 和 ASP.Net MVC 的项目中使用 Ninject.我正在使用 Ninject 2,但是当我使用 Ninject.Web.Mvc 中的 NinjectHttpApplication 时,它会在我使用诸如未创建内核的 PageBase 之类的东西时抱怨.

I want to use Ninject in a project which combines ASP.Net webforms and ASP.Net MVC. I'm using Ninject 2, but when I use NinjectHttpApplication from Ninject.Web.Mvc it complains when I use somethings like a PageBase that the Kernel is not created.

我在 Global.asax 中有以下内容,但不确定要添加什么.

I have the following in the Global.asax and am unsure what to add.

public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication
{
    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
        RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    }

    protected override Ninject.IKernel CreateKernel()
    {
        return new StandardKernel(new ServiceModule());
    }
}

有人在某处工作过,可以分享一些想法或代码吗?

Does somebody has this working somewhere who could share some thoughts or code on this?

推荐答案

正如 Ruben 所说,我已经在 Ninject 邮件列表中发布了一条消息:

As Ruben told, I've put up a message on the Ninject Mailing list:

http://groups.google.com/group/ninject/browse_thread/thread/317fc48387399aa6

简而言之,答案是,不幸的是,这是不可能的.但是,使用自定义 PageBase 类,您可以使属性和方法注入成为可能(来自 Ninject 邮件列表中 Nate Kohari 的回答):

The answer in short is, it unfortunately isn't really possible. However with a custom PageBase class you can make Property and Method injection possible (from Nate Kohari's answer in the Ninject Mailing List):

public abstract class PageBase : Page
{
  public IKernel Kernel { get; private set; }
  public PageBase() { Kernel = ...; }
  public void Page_Init() { Kernel.Inject(this); }
} 

这篇关于Ninject 与 ASP.Net webforms 和 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆