2
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)如果从未执行过action 方法FirstAction.OnActionExecuted,那么调用的目的是什么?Index

b)无论如何,既然FirstAction.OnActionExecuted确实被调用了,为什么不SecondAction.OnActionExecuted也被执行?

4

0 回答 0