Windsor 正在处理静态文件,如果未找到,则会抛出错误

发布于 12-06 02:31 字数 1060 浏览 1 评论 0 原文

我刚刚注意到静态文件正在被处理(我猜这是正常的),但问题是如果文件不存在,它似乎会在这里引起异常:

public class WindsorControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _kernel;

        public WindsorControllerFactory(IKernel kernel)
        {
            this._kernel = kernel;
        }

        public override void ReleaseController(IController controller)
        {
            _kernel.ReleaseComponent(controller);
        }

        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.", requestContext.HttpContext.Request.Path));
            }
            return (IController)_kernel.Resolve(controllerType);
        }
    }

特别是 GetControllerInstance。

这有道理吗?我应该在那里进行额外的检查以确保它是一个正在处理的类吗?

错误:

The controller for path '/Assets/img/logo.png' could not be found.

I just noticed that static files are being processed (I guess that is normal) but the problem is if a file doesn't exist it seems to be causing an exception here:

public class WindsorControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _kernel;

        public WindsorControllerFactory(IKernel kernel)
        {
            this._kernel = kernel;
        }

        public override void ReleaseController(IController controller)
        {
            _kernel.ReleaseComponent(controller);
        }

        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.", requestContext.HttpContext.Request.Path));
            }
            return (IController)_kernel.Resolve(controllerType);
        }
    }

Specifically the GetControllerInstance.

Does this make sense? Should I put extra checks in there to make sure it is a class being processed?

The error:

The controller for path '/Assets/img/logo.png' could not be found.

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

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

发布评论

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

评论(2

也只是曾经 2024-12-13 02:31:05

您可以尝试 排除静态文件由 ASP.NET MVC 引擎处理以提高性能。

You may try excluding static files from being processed by ASP.NET MVC engine to improve performance.

人间☆小暴躁 2024-12-13 02:31:05

如果controllerType为null,则将其传递给基类或返回null

public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel _kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
        this._kernel = kernel;
    }

    public override void ReleaseController(IController controller)
    {
        _kernel.ReleaseComponent(controller);
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
            return base.GetControllerInstance(requestContext, controllerType);

        return (IController)_kernel.Resolve(controllerType);
    }
}

Ninjet示例:"="">https://github.com/ninject/ninject.web.mvc/blob/master/mvc2/src/Ninject.Web.Mvc/NinjectControllerFactory.cs

If controllerType is null, pass it to the base class or return null

public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel _kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
        this._kernel = kernel;
    }

    public override void ReleaseController(IController controller)
    {
        _kernel.ReleaseComponent(controller);
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
            return base.GetControllerInstance(requestContext, controllerType);

        return (IController)_kernel.Resolve(controllerType);
    }
}

Ninjet sample: https://github.com/ninject/ninject.web.mvc/blob/master/mvc2/src/Ninject.Web.Mvc/NinjectControllerFactory.cs

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