MVC 中的 IoC Castle Windsor 路由问题
我已经在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是很自然的。不存在的图像或CSS无法找到控制器,但您将其默认为HomeController,而该控制器无法处理静态内容。
我认为你不需要在这里重写。让默认控制器处理它所做的事情,如果找不到资源,则会收到 404 错误,而不是强制它由该控制器提供服务。
正如我所说,如果找不到该类型,那么该类型很自然地为 null。
改成这样:
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:
我发现当controllerType为null时我必须返回null。将其交给基类会导致异常。下面是我正在使用的工作代码。
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.