public
Created

SingleAction Windsor Controller Factory

  • Download Gist
windsor.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
 
public WindsorControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
 
public override void ReleaseController(IController controller)
{
kernel.ReleaseComponent(controller);
}
 
protected override IController GetControllerInstance(RequestContext context, Type controllerType)
{
var baseNs = typeof(SecureController).Namespace;
 
var ns = context.RouteData.GetRequiredString("controller").ToLower();
var controllerName = context.RouteData.GetRequiredString("action").ToLower() + "controller";
try
{
var controller = kernel.Resolve<IController>(baseNs.ToLower() + "." + ns + "." + controllerName);
return controller;
}
catch (ComponentNotFoundException ex)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", context.HttpContext.Request.Path));
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.