Ninject ... 404 Error Issues

We use our favorite Ninject + Ninject.Web.Mvc with MVC 2 and run into some problems. In particular, it deals with 404 errors. We have a registration service that records 500 errors and records them. Everything works fine only when we try to introduce a non-existent controller. Instead of getting the right 404, we get an error of 500:

Cannot be null Parameter name: service [ArgumentNullException: Cannot be null Parameter name: service] Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional) +188 Ninject.ResolutionExtensions.TryGet(IResolutionRoot root, Type service, IParameter[] parameters) +15 Ninject.Web.Mvc.NinjectControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +36 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +68 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +118 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +46 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +63 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +13 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679426 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

I did some searching and found some similar problems, but these 404 problems do not seem to be related. Any help here would be great.

Thanks! Josh

+4
source share
2 answers

EDIT: Now it is on the trunk for MVC2: http://github.com/enkari/ninject.web.mvc

controllerType is now null, we can pass it to the database and let 404 execute correctly:

  protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { if(controllerType == null) { // let the base handle 404 errors with proper culture information return base.GetControllerInstance(requestContext, controllerType); } var controller = Kernel.TryGet(controllerType) as IController; if (controller == null) return base.GetControllerInstance(requestContext, controllerType); var standardController = controller as Controller; if (standardController != null) standardController.ActionInvoker = CreateActionInvoker(); return controller; } 
+4
source

You need to change the source code of NinjectControllerFactory.cs by adding 404. I have added the source code for anyone interested in fixing it:

  protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { if (controllerType == null) throw new HttpException( 404, String.Format( "The controller for path '{0}' could not be found " + "or it does not implement IController.", requestContext.HttpContext.Request.Path)); var controller = Kernel.TryGet(controllerType) as IController; ... 
+2
source

Source: https://habr.com/ru/post/1304917/


All Articles