Created
May 24, 2011 21:25
-
-
Save Scooletz/989733 to your computer and use it in GitHub Desktop.
ActionProviders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
namespace ActionProviders | |
{ | |
/// <summary> | |
/// The extended controller actions invoker, using other than controllers action providers. | |
/// </summary> | |
public class ActionInvoker : ControllerActionInvoker | |
{ | |
private readonly IActionProvider[] _actionProviders; | |
public ActionInvoker(IActionProvider[] actionProviders) | |
{ | |
_actionProviders = actionProviders; | |
} | |
protected override ActionDescriptor FindAction(ControllerContext ctx, ControllerDescriptor descriptor, string actionName) | |
{ | |
var result = base.FindAction(ctx, descriptor, actionName); | |
if (result != null) | |
return result; | |
// not found, search through providers | |
foreach (var provider in _actionProviders) | |
{ | |
result = provider.FindAction(ctx, descriptor, actionName); | |
if (result != null) | |
return result; | |
} | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Castle.Windsor; | |
namespace ActionProviders | |
{ | |
public class ControllerFactory : DefaultControllerFactory | |
{ | |
private readonly IWindsorContainer _container; | |
private readonly IActionInvoker _invoker; | |
public ControllerFactory(IWindsorContainer container, IActionInvoker invoker) | |
{ | |
_container = container; | |
_invoker = invoker; | |
} | |
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) | |
{ | |
try | |
{ | |
var controller = (Controller)_container.Resolve(controllerType); | |
controller.ActionInvoker = _invoker; | |
return controller; | |
} | |
catch (Exception ex) | |
{ | |
throw new InvalidOperationException("The controller of type " + controllerType.FullName + " could not be instantiated.", ex); | |
} | |
} | |
public override void ReleaseController(IController controller) | |
{ | |
_container.Release(controller); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
namespace ActionProviders | |
{ | |
/// <summary> | |
/// The interface for objects, which can provide an action where none matching is found onto a controller. | |
/// </summary> | |
public interface IActionProvider | |
{ | |
ActionDescriptor FindAction(ControllerContext ctx, ControllerDescriptor descriptor, string actionName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment