首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尼尼姆: ninject.web -如何在常规的ASP.Net网站上应用(!MVC)

尼尼姆: ninject.web -如何在常规的ASP.Net网站上应用(!MVC)
EN

Stack Overflow用户
提问于 2010-06-17 05:47:56
回答 2查看 2.9K关注 0票数 1

我所看到的是类似于下面(http://github.com/ninject/ninject.web.mvc)的内容:

README.markdown

这个扩展允许尼尼姆核心和ASP.NET MVC项目之间的集成。要使用它,只需使HttpApplication (通常在Global.asax.cs中)扩展NinjectHttpApplication:

公共类YourWebApplication : NinjectHttpApplication { public覆盖void () { //这仅在RegisterAllControllersIn("Some.Assembly.Name");MVC1 }中需要 公共覆盖IKernel CreateKernel() {返回新StandardKernel(新的SomeModule()、新的SomeOtherModule()、.); // OR,自动加载模块: var内核=新的StandardKernel();kernel.AutoLoadModules("~/bin");返回内核;}

完成此操作后,控制器将通过Ninject被激活,这意味着您可以公开对其构造函数(或属性或方法)的依赖关系,以请求注入。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-06-23 10:00:01

因为你在问题中没有排除这一点,我不得不假设你没有意识到与您引用的WebForms等价的

(从主站点上的Ninject扩展索引链接)

票数 1
EN

Stack Overflow用户

发布于 2010-06-29 03:12:50

只是想分享一下我是如何用2008解决它的

对于你们这些人来说,下面的www.tekpub.com代码是很熟悉的,是的!下面正确的代码来自精通ASP.NET MVC 2.0系列,以及如何使用NLog的演示

需要参考:

  • Ninject.dll
  • Ninject.Web
  • NLog.dll

Global.asax :

代码语言:javascript
复制
<%@ Application Language="C#" Inherits ="Ninject.Web.NinjectHttpApplication"  %>
<%@ Import Namespace="App_Code.Infrastructure.Logging"%>
<%@ Import Namespace="Ninject.Modules"%>
<%@ Import Namespace="Ninject"%>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }


    protected override void OnApplicationStarted()
    {
        //base.OnApplicationStarted();

        Container.Get<ILogger>().Info("Application Started");
    }

    protected override IKernel CreateKernel()
    {
        return Container;
    }

    static IKernel Container
    {
        get
        {
            return new StandardKernel(new SiteModule());
        }

    }

    class SiteModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ILogger>().To<NLogger>().InSingletonScope();
        }
    }

</script>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3059187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档