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

时间:2011-09-24 15:20:06

标签: asp.net-mvc castle-windsor

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

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.

2 个答案:

答案 0 :(得分:0)

您可以尝试使用ASP.NET MVC引擎处理excluding static files来提高性能。

答案 1 :(得分:0)

如果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