Castle Windsor with MVC 2.0 and Areas
Wanted to ask a quick question regarding castle windsor and implementing IoC for Controllers in Areas. Does Castle 2.5 support MVC 2.0 areas?
My Castle config works ok for my root controller in the root of my site but any area controllers are not found with a
The IControllerFactory 'XXX.Castle.WindsorControllerFactory' did not return a controller for the name 'Registration'.
I am using Castle directly not through MvcContrib
Code as follows:
class WindsorControllerFactory : DefaultControllerFactory
{
WindsorContainer container;
// The constructor:
// 1. Sets up a new IoC container
// 2. Registers all components specified in web.config
// 3. Registers all controller types as components
public WindsorControllerFactory()
{
// Instantiate a container, taking configuration from web.config
container = new WindsorContainer();
// Also register all the controller types as transient
var controllerTypes =
from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes) {
//container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
container.Register(Component.For(t).Named(t.FullName).LifeStyle.Transient);
}
container.Install(new WindsorInstaller());
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType != null)
{
return (IController)container.Resolve(controllerType);
}
return null;// base.GetControllerInstance(requestContext, controllerType);
}
}
Many thanks
Richard
For those who meet this issue in the future I have a solution that fixed my issue. The issue was that my controllers did not have the correct namespace allocated to the directory they were in..
ie I had tsd.Web.Controllers NOT tsd.Areas.Account.Controllers
Setting the namespace path to map the directory structure solved my issue and the castle could then located the controller in the area...!
Regards
Richard
你可以返回任何控制器,然后你可以得到异常404
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType != null)
{
return (IController)container.Resolve(controllerType);
}
return (IController)container.Resolve(typeof(HomeController));
}
链接地址: http://www.djcxy.com/p/59862.html
上一篇: mvc和城堡温莎
下一篇: MVC 2.0和地区温莎城堡