Wiecej informacji na ten temat tutaj
Tworzymy własny ActionInvoker:
/// <summary>
/// Custom Action Invoker. The ControllerActionInvoker is responsible for creating the parameters that are passed to a controller action.
/// </summary>
public class ContextActionInvoker : ControllerActionInvoker
{
public ContextActionInvoker(ControllerContext controllerContext)
: base()
{
}
/// <summary>
/// Invokes the specified action. Override to log browsing.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="actionName">The name of the action.</param>
/// <returns></returns>
public override bool InvokeAction(ControllerContext controllerContext, string actionName)
{
HttpContextBase context = controllerContext.HttpContext;
//Do sth
return base.InvokeAction(controllerContext, actionName);
}
}
Tworzymy własny ControllerFactory. Przeładowywujemy funkcję GetControllerInstance, w której przypisujemy instancję własnego invoker'a.
/// <summary>
/// Custom Controller Factory
/// </summary>
public class ContextControllerFactory : DefaultControllerFactory
{
/// <summary>
/// Gets the controller instance. Override to put down custom ActionInvoker to ActionInvoker
/// </summary>
/// <param name="controllerType">Type of the controller.</param>
/// <returns>A reference to the controller.</returns>
protected override IController GetControllerInstance(Type controllerType)
{
IController controller = base.GetControllerInstance(controllerType);
Controller contextController = controller as Controller;
if (contextController != null)
{
var context = new ControllerContext(this.RequestContext, contextController);
contextController.ActionInvoker = new ContextActionInvoker(context);
}
return controller;
}
}
CustomControllerFactory rejestrujemy w global.asax.cs
protected void Application_Start()
{
//Register new Controller Factory
ControllerBuilder.Current.SetControllerFactory(typeof(ContextControllerFactory));
RegisterRoutes(RouteTable.Routes);
}
Brak komentarzy:
Prześlij komentarz