问题描述
在MVC中,我们可以用不同的过滤器装饰动作方法,比如
In MVC we can decorate action methods with different filters like
[HttpPost]
[Authorize]
public ActionResult mymethod(){}
HttpPost
派生自 MethodSelectorAttribute
(可能是间接的),而 Authorize
属性继承自 ActionFilterAttribute
.
HttpPost
derives from MethodSelectorAttribute
(probably indirectly) and the Authorize
attribute inherits from ActionFilterAttribute
.
我的问题是:它们在 MVC 请求管道中以什么顺序执行?我试图在 MVC 源代码中搜索但未能找到相关的代码位.
My question is: in which order are they executed in the MVC request pipeline? I tried to go search in MVC source code but failed to find the relevant code bits.
推荐答案
过滤器按以下顺序运行:
Filters run in the following order:
- 授权过滤器
- 动作过滤器
- 响应过滤器
- 异常过滤器
例如,授权过滤器首先运行,异常过滤器最后运行.在每个过滤器类型中,Order 值指定运行顺序.在每个过滤器类型和顺序中,Scope 枚举值指定过滤器的顺序.此枚举定义了以下过滤器范围值(按它们运行的顺序):
For example, authorization filters run first and exception filters run last. Within each filter type, the Order value specifies the run order. Within each filter type and order, the Scope enumeration value specifies the order for filters. This enumeration defines the following filter scope values (in the order in which they run):
- 首先
- 全球
- 控制器
- 行动
- 最后
摘自 MSDN
这篇关于过滤器在 asp.net mvc 中执行的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!