Castle ControllerFactory Samples that don't work with MVC Areas
Following the samples that appear to be targeting MVC 3.0 and Castle.Windsor
I've run into a bit of a snag - I know a workaround, but I am really confused about what the 'best practice' to do is now.
http://stw.castleproject.org/(S(su2mgm45fclhqe55veo40545))/Windsor.MainPage.ashx
In this example, the author constructs a WindsorControllerFactory
. [ http://stw.castleproject.org/(S(su2mgm45fclhqe55veo40545))/Windsor.Windsor-tutorial-part-two-plugging-Windsor-in.ashx ]
Here is the code he uses ...
public class WindsorControllerFactory: DefaultControllerFactory
{
private readonly IKernel kernel;
public WindsorControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
public override void ReleaseController(IController controller)
{
kernel.ReleaseComponent(controller);
}
public override IController CreateController(RequestContext requestContex开发者_Go百科t, string controllerName)
{
var controllerComponentName = controllerName + "Controller";
return kernel.Resolve<IController>(controllerComponentName);
}
}
Now, this does work ...sort of. The problem comes with any ASP.NET MVC
application that has Areas involved. The controllerName doesn't work there.
In other ControllerFactory implementations I have seen, people use the overrided method GetControllerInstance
in a fashion like this ...
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found or it does not implement IController.", requestContext.HttpContext.Request.Path));
return (IController)_container.Resolve(controllerType);
}
So I am confused. What is considered the best approach? CreateController
or GetControllerInstance
?
And before anyone brings up MVCContrib - I've explored it, and to be very frank, I must be either ridiculously stupid, or just missing the point, because I can't even find the ControllerFactory in it - and in the binaries I have downloaded for it, I couldn't get anything wired up. I understand this project is a very good one, not slamming it - but I see it referenced a lot and I can't find what people are talking about.
Can anyone help me clear up this mystery? Why did the author of this tutorial show us a ControllerFactory that won't work with Areas? How could it be made to work with them? Why do some use CreateController
and some use GetControllerInstance
? Any information is excessively appreciated.
Actually you're right. The existing implementation was OK in MVC 1 days before areas were introduced. Currently the GetControllerInstance
method should be overriden to accommodate that. Feel free to fix it and submit a pull request.
精彩评论