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:

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.

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?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?