使用 ASP.Net Webforms 和 MVC 的 Ninject

发布于 2024-09-07 15:45:38 字数 677 浏览 5 评论 0原文

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

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

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());
    }
}

有人在某个地方有这个工作可以分享一些想法或代码吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放肆 2024-09-14 15:45:38

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

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

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

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

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

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

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); }
} 
℉服软 2024-09-14 15:45:38

1) 查看 Mvc 和非 Mvc Ninject 扩展的源代码 - 代码非常简短而整洁

2) 转到 ninject 邮件列表并提出这个问题,以及您从源代码中学到的内容。很快就会有答案或补丁

1) Have a look in the source for both the Mvc and non-Mvc Ninject Extensions - the code is very short and neat

2) Go to the ninject mailing list and ask this question, together with what you've learned from the source. There'll be an answer or a patch lightening quick

南冥有猫 2024-09-14 15:45:38

这里有一些关于如何在 Web 表单中利用 DI 的非常好的资源,我之前已经将它们添加为书签:

http://aspnetresources .com/articles/ioc_and_di_with_web_forms
希望这

有帮助。

here are some very good resources on how to leverage DI in webforms that I had bookmarked some time ago:

http://aspnetresources.com/articles/ioc_and_di_with_web_forms
http://aspnetresources.com/articles/real_world_ioc_and_di_with_webforms

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文