过滤器在 asp.net mvc 中执行的顺序 [英] In what order are filters executed in asp.net mvc

查看:25
本文介绍了过滤器在 asp.net mvc 中执行的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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:

  1. 授权过滤器
  2. 动作过滤器
  3. 响应过滤器
  4. 异常过滤器

例如,授权过滤器首先运行,异常过滤器最后运行.在每个过滤器类型中,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):

  1. 首先
  2. 全球
  3. 控制器
  4. 行动
  5. 最后

摘自 MSDN

这篇关于过滤器在 asp.net mvc 中执行的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆