Getting started with Ninject

前端 未结 3 2004
傲寒
傲寒 2020-12-29 14:37

I watched the first 2 beginner tutorials for Ninject on dimecasts.net. Now, I want to use Ninject 2.2 in ASP.NET MVC 3. I want a view with a mocked out Model. I get objec

3条回答
  •  误落风尘
    2020-12-29 14:54

    Try this in your global.asax file:

    //By using the NinjectHttpApplication, it automatically takes care of controllers, starting up ninject, etc.
    //Ninject.Web.Mvc
    public class MvcApplication : NinjectHttpApplication
    {
        //Your other stuff here. No need to call StartNinject().
    
        #region Inversion of Control
    
        protected override IKernel CreateKernel()
        {
            return Container;
        }
    
        static IKernel _container;
        public static IKernel Container
        {
            get
            {
                if (_container == null)
                {
                    _container = new StandardKernel(new SiteModule());
                }
                return _container;
            }
        }
    
        internal class SiteModule : NinjectModule
        {
            public override void Load()
            {
                //Set up ninject bindings here.
                Bind().To();
            }
        }
        #endregion
    }
    

提交回复
热议问题