Ninject 问题...404 错误问题

发布于 2024-08-26 15:33:01 字数 1581 浏览 5 评论 0原文

我们正在使用深受喜爱的 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 问题似乎不相关。这里的任何帮助都会很棒。

谢谢! 乔什

We are using the beloved Ninject+Ninject.Web.Mvc with MVC 2 and are running into some problems. Specifically dealing with 404 errors. We have a logging service that logs 500 errors and records them. Everything is chugging along just perfectly except for when we attempt to enter a non-existent controller. Instead of getting the desired 404 we end up with a 500 error:

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 issues, but those 404 issues seem to be unrelated. Any help here would be great.

Thanks!
Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-09-02 15:33:01

编辑:现在位于 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;
    }

EDIT: This is in the trunk now for MVC2: http://github.com/enkari/ninject.web.mvc

controllerType is coming in null now, we can pass it to the base and let the 404 happen properly:

        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;
    }
七秒鱼° 2024-09-02 15:33:01

需要通过添加 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;

        ...

Needed to modify NinjectControllerFactory.cs source code by adding the 404. I have added the source code for anyone interested in fixing:

    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;

        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文