IoC Castle Windsor in MVC routing problem

∥☆過路亽.° 提交于 2019-12-06 15:24:06

This is only natural. The non-existing image or css cannot find the controller but you are defaulting it to the HomeController while this controller cannot handle static content.

I do not think you need an override here. Let the default controller handle what it does and resource will get a 404 error if it cannot be found instead you forcing it to be served by that controller.


As I said, it is only natural for the type to be null if it cannot be found. Change it to this:

   if (controllerType == null)
    {
        return base.GetControllerInstance(requestContext, controllerType);

    }

I found that I had to return null when the controllerType was null. Handing it on to the base class resulted in an exception. Below is the working code that I am using.

public class DependencyControllerFactory : DefaultControllerFactory, IDisposable
{
    protected readonly WindsorContainer _container;

    public DependencyControllerFactory()
    {
        _container = new WindsorContainer();

        _container.Kernel.Resolver.AddSubResolver(new CollectionResolver(_container.Kernel));
        _container.Install(FromAssembly.This());
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            return null;
        }
        else
        {
            return (IController)_container.Resolve(controllerType);
        }
    }

    public override void ReleaseController(IController controller)
    {
        _container.Release(controller);
    }

    public void Dispose()
    {
        _container.Dispose();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!