Setup Ninject | MVC

Implement the controller

You first implement the NinjectControllerFactory class that enables Sitefinity CMS and the Classic MVC mode to instantiate controllers that may accept parameters in their constructors:

C#
using System;
using System.Web.Mvc;
using Ninject;
using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers;

namespace SitefinityWebApp
{
    public class NinjectControllerFactory : FrontendControllerFactory
    {
        public NinjectControllerFactory()
        {
            this.ninjectKernel = Global.NinjectKernel;
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                return null;
            }

            var resolvedController = this.ninjectKernel.Get(controllerType);
            IController controller = resolvedController as IController;

            return controller;
        }

        private readonly IKernel ninjectKernel;
    }
}

Register the controller

Next, you implement the Global.asax file that registers the NinjectConrtollerFactory that enables constructor injection in Controllers. This code example demonstrates how to register a new dependency resolver that enables the constructor dependency injection in ApiControllers. In addition, you register routes that enable working in Classic MVC mode and making Web API calls.

C#
using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Web.Common;
using Ninject.Web.WebApi;
using Telerik.Microsoft.Practices.Unity;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Mvc;

namespace SitefinityWebApp
{
    public class Global : NinjectHttpApplication
    {
        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            Telerik.Sitefinity.Abstractions.Bootstrapper.Bootstrapped += this.Bootstrapper_Bootstrapped;
        }

        protected override IKernel CreateKernel()
        {
            IKernel kernel = new StandardKernel();
            NinjectKernel = kernel;
            return kernel;
        }

        public static IKernel NinjectKernel { get; private set; }

        protected void Bootstrapper_Bootstrapped(object sender, EventArgs e)
        {
            // Account for constructor injection in Controller types
            // This controller factory initialization accounts for both MVC (Feather) widgets, and Classic MVC Mode Controllers
            ObjectFactory.Container.RegisterType<ISitefinityControllerFactory, NinjectControllerFactory>(new ContainerControlledLifetimeManager());
            ISitefinityControllerFactory factory = ObjectFactory.Resolve<ISitefinityControllerFactory>();
            ControllerBuilder.Current.SetControllerFactory(factory);

            // Account for constructor injection in ApiController types
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(NinjectKernel);

            this.RegisterWebApiRoute();
            this.RegisterClassicMvcModeRoute();
        }

        /// <summary>
        /// Enables WebApi calls
        /// </summary>
        /// <remarks>
        /// This registration does not depend on Ninject and does not account for constructor injection
        /// </remarks>
        private void RegisterWebApiRoute()
        {
            GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                "DefaultApi",
                "webapi/{controller}/{id}",
                new { id = RouteParameter.Optional });
        }

        /// <summary>
        /// Enables Classic MVC Mode
        /// </summary>
        /// <remarks>
        /// This registration does not depend on Ninject and does not account for constructor injection
        /// </remarks>
        private void RegisterClassicMvcModeRoute()
        {
            RouteTable.Routes.MapRoute(
                "Classic",
                "classic/{controller}/{action}/{id}",
                new { controller = "Feature", action = "Index", id = RouteParameter.Optional });
        }

        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

For more information, see Classic MVC mode.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.