使用ControllerContext动态调用操作方法时,ActionFilterAttribute不触发

时间:2019-04-07 20:12:49

标签: c# asp.net-mvc actionfilterattribute custom-action-filter controllercontext

我有一个要求,我需要在控制器上动态初始化和调用一个动作方法。我正在使用依赖注入来获取控制器实例,然后创建如下的controllercontext。当我遵循这种方法时,ACustomAttribute中的“ OnActionExecuting”根本不会被触发。

var aController = ServiceLocator.Current.Resolve<AController>();
aController.ControllerContext = new ControllerContext(this.Request.RequestContext, aController);
aController.PostActionMethod();

//Action method has an attribute.
[ACustom]
public ActionResult PostActionMethod() {}

//Custom attribute looks like this
public sealed class ACustomAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
       ////Doesn't trigger...
    }
}

另一方面,如果我直接从客户端ajax调用PostActionMethod,则ACustomAttribute触发并且一切正常。

知道动态调用时我在做什么错吗?

0 个答案:

没有答案
相关问题