MVC 中的 IoC Castle Windsor 路由问题

发布于 2024-10-12 06:06:31 字数 2270 浏览 5 评论 0原文

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

<link rel="stylesheet" type="text/css" href="non-existing.css"/>

或者指向不存在的图像的链接。为什么会发生这种情况?

我的窗户课程:

    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
}

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?

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
}

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

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

发布评论

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

评论(2

请止步禁区 2024-10-19 06:06:31

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

我认为你不需要在这里重写。让默认控制器处理它所做的事情,如果找不到资源,则会收到 404 错误,而不是强制它由该控制器提供服务。


正如我所说,如果找不到该类型,那么该类型很自然地为 null。
改成这样:

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

    }

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

    }
爱你是孤单的心事 2024-10-19 06:06:31

我发现当controllerType为null时我必须返回null。将其交给基类会导致异常。下面是我正在使用的工作代码。

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

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