我有2个动作过滤器" ActionFilter1" &安培; " ActionFilter2,
public class ActionFilter1 : FilterAttribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext.Current.Application["Order"] += "Action 1: OnActionExecuting <br/>";
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
HttpContext.Current.Application["Order"] += "Action 1: OnActionExecuted <br/>";
}
}
public class ActionFilter2 : FilterAttribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext.Current.Application["Order"] += "Action 2: OnActionExecuting <br/>";
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
HttpContext.Current.Application["Order"] += "Action 2: OnActionExecuted <br/>";
}
}
我使用&#34; Order&#34;,
在控制器操作方法上应用了两个过滤器[ActionFilter1(Order = 1)]
[ActionFilter2(Order = 2)]
public ActionResult Index()
{
return View();
}
当Page在浏览器上执行时,我得到了动作过滤器执行的输出,
行动1:OnActionExecuting
行动2:OnActionExecuting
行动2:OnActionExecuted
行动1:OnActionExecuted
问题 - 为什么&#34; OnActionExecuted&#34;以相反的顺序执行,同时&#34; OnActionExecuting&#34;我在属性中提供的顺序是否正确?
谢谢,
答案 0 :(得分:3)
这是记录在案的行为。这是链接on MSDN:
OnActionExecuting(ActionExecutingContext), OnResultExecuting(ResultExecutingContext),和 OnAuthorization(AuthorizationContext)过滤器以正向顺序运行。 OnActionExecuted(ActionExecutedContext), OnResultExecuting(ResultExecutingContext),和 OnException(ExceptionContext)过滤器以相反的顺序运行。