public class FirstActionAttribute : FilterAttribute, IActionFilter
{
/* Is Called */
public void OnActionExecuted(ActionExecutedContext filterContext)
{}
public void OnActionExecuting(ActionExecutingContext filterContext)
{}
}
public class SecondActionAttribute : FilterAttribute, IActionFilter
{
/* Isn't Called */
public void OnActionExecuted(ActionExecutedContext filterContext)
{}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Result = new ViewResult { ViewName = "NotIndex" };
}
}
[FirstAction(Order =1)]
[SecondAction(Order=2)]
public ActionResult Index()
{
return View();
}
OnActionExecuted()
被调用用于在设置OnActionExecuting()
属性的操作过滤器之前调用Result
的操作过滤器,因此取消了操作方法的执行。在我们的例子中,FirstAction.OnActionExecuted
被调用,而SecondAction.OnActionExecuted
没有被调用。
a)如果 FirstAction.OnActionExecuted
动作方法从未执行过,那么调用Index
的目的是什么?
b)无论如何,由于FirstAction.OnActionExecuted
被调用,为什么不执行SecondAction.OnActionExecuted
呢?