如何将 Ninject.Web.PageBase 与另一个自定义 .NET PageBase 一起使用
我正在尝试让 Ninject 使用已经具有自定义 PageBase 对象的 WebForms 应用程序。但是,我不确定是否可以将 Ninject 的 PageBase 对象与另一个自定义 PageBase 一起使用。我已经搜索了一段时间,看看是否能找到这个问题的答案,或者学习如何做到这一点,但我发现的是:
我已经使用共享基类编写了一个替代方案, 源自页面。看起来大概是这样的
公共抽象类PageBase:页面 { 公共 IKernel 内核 { 获取;私人套装; } 公共 PageBase() { 内核 = ...; } 公共无效 Page_Init() { Kernel.Inject(this); } }
这将允许您在任何页面上进行属性和方法注入 继承自PageBase。请注意,构造函数不完整—— 您必须以某种静态方式访问内核。你应该 能够以某种方式从 HttpApplication 读取它。
(来源:http://groups.google.com/group/ninject/browse_thread /thread/317fc48387399aa6,链接自Ninject with ASP.Net webforms and MVC):
这看起来就像它可能对我有用,因为我似乎可以将此代码应用到现有的自定义 PageBase。但是,我对作者所说的部分感到困惑,“......构造函数不完整 - 你必须以某种静态方式访问内核。”
有谁知道这句话的含义,以及如何以静态方式访问 Ninject 内核?
I am trying to get Ninject working with a WebForms application that already has a custom PageBase object. But, I don't know for sure if I can use Ninject's PageBase object alongside another, custom PageBase. I've been searching for a while now to see if I could find an answer to this problem, or to learn how to do it, but all I've found is this:
I've hacked together an alternative using a shared base class that
derives from Page. It looks roughly like thispublic abstract class PageBase : Page { public IKernel Kernel { get; private set; } public PageBase() { Kernel = ...; } public void Page_Init() { Kernel.Inject(this); } }
This will allow you to property and method injection on any pages that
inherit from PageBase. Note that the constructor is incomplete --
you'll have to access the kernel in some static fashion. You should
be able to read it from the HttpApplication somehow.
(source: http://groups.google.com/group/ninject/browse_thread/thread/317fc48387399aa6, linked from Ninject with ASP.Net webforms and MVC):
This looks like it might work for me because it appears that I could apply this code to the existing, custom PageBase. But, I am hung up on the part in which the author says, "... the constructor is incomplete -- you'll have to access the kernel in some static fashion."
Does anyone have any idea what that sentence means, and how one might go about accessing the Ninject kernel in a static fashion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要从 Ninject 页面库派生。您也可以使用 NinjectHttpModule。
https://github.com/ninject/ninject .web/blob/master/src/Ninject.Web/NinjectHttpModule.cs
You do not need to derive from a Ninject page base. You can alternatively use the NinjectHttpModule.
https://github.com/ninject/ninject.web/blob/master/src/Ninject.Web/NinjectHttpModule.cs