IoC Castle Windsor在MVC中的路由问题 [英] IoC Castle Windsor in MVC routing problem

查看:70
本文介绍了IoC Castle Windsor在MVC中的路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的mvc应用程序中设置了城堡温莎.一切都很好,除了它还能捕获链接或图像类型的路由.问题是,在退出控制器并生成视图"GetControllerInstance"之前,将以"null"类型执行该问题.每当页面上有链接时,就会发生这种情况:

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="stylesheet" type="text/css" href="non-existing.css"/>

或指向不存在的图像的链接.为什么会这样?

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

我的Windows类:

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
}

推荐答案

这是很自然的.不存在的图像或css找不到控制器,但您默认将其设置为HomeController,而此控制器无法处理静态内容.

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.

我认为您不需要在这里覆盖.让默认控制器处理它的工作,如果找不到资源,则资源将显示404错误,而是您强制该控制器为它提供服务.

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.

正如我所说,如果找不到类型,则将其自然为null是很自然的. 更改为此:

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);

    }

这篇关于IoC Castle Windsor在MVC中的路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆