I have created an ASP.NET MVC application and am trying to use Castle Windsor as my IOC
However, when the controllers are trying to resolve I am getting 'Content' and 'Scripts' into the 'controllerName' parameter in the CreateController(RequestContext requestContext, string controllerName)
method. Needless to say these are not controllers. They appears to be the folders of the web site
Why is it trying to register these as controllers?
How do I ignore these folders??
thanks
exception from WindsorControllerFactory
Due to not being able to post the image I have to describe it - it basically just says
'The contentcontroller was not found'
Global.asax.cs
public static IIocContainer Ioc;
protected void Application_Start()
{
InitialiseIocContainer();
RegisterViewEngine(ViewEngines.Engines);
RegisterRoutes(RouteTable.Routes);
StartProfiling();
}
private void InitialiseIocContainer()
{
IWindsorContainer _container = new WindsorContainer();
var controllerTypes = typeof (GidgetController).Assembly.GetTypes();
foreach (var controllerType in controllerTypes.Where((t=>typeof(IController).IsAssignableFrom(t))))
{
_container.AddComponentLifeStyle(controllerType.Name.ToLower(), controllerType, LifestyleType.Transient);
}
_container.AddComponent("a",typeof(IGidgetService), typeof(GidgetService));
_container.AddComponent("b",typeof(IGidgetRepository), typeof(GidgetRepository));
_container.AddComponent("c",typeof(IGidgetValidator), typeof(GidgetValidator));
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
}
windsorControllerFactory.cs
public IController CreateController(RequestContext requestContext, string controllerName)
{
try
{
controllerName = controllerName.ToLower() + "controller";
var controller = _container.Resolve<IController>(controllerName);
return controller;
}
catch (ComponentNotFoundException)
{
throw new HttpException(404, string.Format("The {0} controller was not found", controllerName));
}
}
Just use MVCContrib.
It includes everything to integrate Windsor and ASP.NET MVC, including the controller factory and extensions to register controllers.
I follow Steve Sanderson's pattern in creating a WindsorControllerFactory, which works quite well. I've modified his to actually return proper 404s when there isn't a controller by the name (i.e. someone types "/garbageblahblah") because his pattern didn't pick it up. So, I use Reflector to grab the bits that are built into the default controller factory for handling bad urls.
His pattern uses reflection to find all controllers within your Mvc project, and register them all upon app startup. You don't want to use CreateController, but instead GetControllerInstance() as this is only called by the underlying Mvc framework when a controller is going to be invoked. /Content is ignored by convention, and therefore is not called.
My CastleWindsorControllerFactory:
And within my Globals.asax.cs, this is all you need (what you have above is vast overkill! I already loop through and register them within my CastleWindsorControllerFactor above).
Technically, this isn't exactly the code I use. I have actually abstracted the entire Castle Windsor off to a static instance in a class I call ComponentFactory. This allows me to have true Singleton patterns through my applications, amongst several strong-type additions.
I mention this, because I plan on releasing ComponentFactory() soon on my blog. But, drop me an email and I'll send the latest version to you. Email is: me -at- eduncan911.com