How can I get this Ninject 2 code to work with Ninject 3?

20 views
Skip to first unread message

Kevin Major

unread,
May 9, 2012, 10:25:11 PM5/9/12
to nin...@googlegroups.com
In my MVC 2 project, I originally used Ninject 2 and wrote this version of the NinjectControllerFactory:

    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel kernel = new StandardKernel(new HandiGamerServices());
 
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            try
            {
                if (controllerType == null)
                {
                    return base.GetControllerInstance(requestContext, controllerType);
                    // return null;
                }
            }
            catch (HttpException ex)
            {
                if (ex.GetHttpCode() == 404)
                {
                    IController errorController = kernel.Get<ErrorController>();
                    ((ErrorController)errorController).InvokeHttp404(requestContext.HttpContext);
 
                    return errorController;
                }
                else
                {
                    throw ex;
                }
            }
 
            return (IController)kernel.Get(controllerType);
        }

Of most importance is the retrieval of my ErrorController, which allows me to gracefully handle a multitude of HTTP errors.

The problem is that I upgraded to the MVC 2 extension via Nuget, so a NinjectControllerFactory is already provided. Would it be possible to use my own override of GetControllerInstance?
Reply all
Reply to author
Forward
0 new messages