I'm using this post to create authorization in my MVC4 project. The problem is that I'm using Castle Windsor as DI. When invoking the function below I get an error on the line return (ControllerBase) controller
static ControllerBase GetControllerByName(
HtmlHelper helper, string controllerName)
{
IControllerFactory factory =
ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(
helper.ViewContext.RequestContext, controllerName);
if (controller == null)
{
throw new InvalidOperationException(
string.Format(
CultureInfo.CurrentUICulture,
"Controller factory {0} controller {1} returned null",
factory.GetType(),
controllerName));
}
return (ControllerBase) controller; // <= error here
}
The error I get is:
Unable to cast object of type
'Castle.Proxies.IControllerProxy_2' to type 'System.Web.Mvc.ControllerBase'.
Is there a way so I can make this work?