我们正在使用心爱的Ninject + Ninject.Web.Mvc和MVC 2,并遇到了一些问题。专门处理404错误。我们有一个日志记录服务,记录500个错误并记录它们。除了当我们试图进入一个不存在的控制器时,一切都在完美地完成。而不是获得所需的404我们最终得到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
我做了一些搜索,发现了一些类似的问题,但那些404问题似乎是无关的。这里的任何帮助都会很棒。
谢谢! 约什
答案 0 :(得分:4)
编辑:现在这是MVC2的主干:http://github.com/enkari/ninject.web.mvc
controllerType现在为null,我们可以将它传递给基础并让404正常发生:
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;
}
答案 1 :(得分:2)
需要通过添加404来修改NinjectControllerFactory.cs源代码。我已经为有兴趣修复的人添加了源代码:
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;
...