Skip to content

Instantly share code, notes, and snippets.

@Scooletz
Created May 24, 2011 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scooletz/989733 to your computer and use it in GitHub Desktop.
Save Scooletz/989733 to your computer and use it in GitHub Desktop.
ActionProviders
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;
}
}
}
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);
}
}
}
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