In my MVC projects, I derive my controllers from a custom base controller in order to better handle situations that would necessitate me to return a 404 or 500 status code. In order to do that, I have my own controller factory that is invoked in OnApplicationStartred(). The only problem is that it requires the Ninject kernel to work, and I can only seem to get it via the deprecated attribute. Code:
HGControllerFactory:
public class HGControllerFactory : IControllerFactory
{
private IControllerFactory defaultFactory;
private IKernel kernel;
public HGControllerFactory(IControllerFactory defaultFactory, IKernel kernel)
{
this.defaultFactory = defaultFactory;
this.kernel = kernel;
}
public IController CreateController(RequestContext requestContext, string controllerName)
{
try
{
var controller = defaultFactory.CreateController(requestContext, controllerName);
return controller;
}
catch (HttpException ex)
{
// Pasted in your exception handling code here:
if (ex.GetHttpCode() == 404)
{
IController errorController = kernel.Get<ErrorController>();
((ErrorController)errorController).InvokeHttp404(requestContext.HttpContext);
return errorController;
}
else
{
throw ex;
}
}
}
public void ReleaseController(IController controller)
{
defaultFactory.ReleaseController(controller);
}
}
OnApplicationStarted():
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new HGControllerFactory(ControllerBuilder.Current.GetControllerFactory(), this.Kernel));
}Is there another way for me to get a hold of the Ninject kernel?