开发者

How to use Ninject.Web.PageBase alongside another, custom .NET PageBase

开发者 https://www.devze.com 2023-03-25 22:03 出处:网络
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

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 this

public 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消