Ninject with ASP.Net webforms and MVC(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Ninject 与 ASP.Net webforms 和 MVC


基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01