开发者

IoC Castle Windsor in MVC routing problem

开发者 https://www.devze.com 2023-02-04 23:55 出处:网络
I\'ve set up castle windsor in my mvc app. everything works great except it also catches routes that are of type link or image. The problem is that right before exiting from the controller and generat

I've set up castle windsor in my mvc app. everything works great except it also catches routes that are of type link or image. The problem is that right before exiting from the controller and generating the view "GetControllerInstance" is executed with 'null' type. This happends anytime there a link on a page like:

<link rel="styles开发者_开发技巧heet" type="text/css" href="non-existing.css"/>

Or a link to an image that does not exist. Why is this happening?

My windows class:

    public class WindsorControllerFactory : DefaultControllerFactory
{
    #region Constants and Fields

    /// <summary>
    /// The container.
    /// </summary>
    private readonly WindsorContainer container;

    #endregion

    // The constructor:
    // 1. Sets up a new IoC container
    // 2. Registers all components specified in web.config
    // 3. Registers all controller types as components
    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="WindsorControllerFactory"/> class.
    /// </summary>
    public WindsorControllerFactory()
    {
        // Instantiate a container, taking configuration from web.config
        this.container = InversionOfControl.Container;

        // Also register all the controller types as transient
        IEnumerable<Type> controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                            where typeof(IController).IsAssignableFrom(t)
                                            select t;
        foreach (Type t in controllerTypes)
        {
            this.container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
        }
    }

    #endregion

    #region Methods

    /// <summary>
    /// The get controller instance.
    /// </summary>
    /// <param name="requestContext">
    /// The request context.
    /// </param>
    /// <param name="controllerType">
    /// The controller type.
    /// </param>
    /// <returns>
    /// Resolved controller instance.
    /// </returns>
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            controllerType = typeof(HomeController);
        }

        return (IController)this.container.Resolve(controllerType);
    }

    #endregion
}


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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消