惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Na57

ASP.NET MVC应用中一个诡异错误的处理 使用Enterprise Library 4.0 Logging 的问题 6. 视图和生成助手 5. 控制器和Action方法 2. 创建基本的MVC项目 3. URL路由 - Na57 - 博客园 4. 用MVC实现URL路由 Google笔记本迈向烂笔头 XML and Databases 笔记 烂笔头又来了 - Na57 - 博客园 <程序员>200711期算法擂台的解答 GeoServer抛异常IllegalArgumentException的原因 CSS学习 关于CSS中float属性的理解 ItemCreated与ItemDataBound 《SAML简介:安全地共享数字身份信息》读后感 JavaScript 学习(2) - JS的内建对象 JavaScript from C#(入门篇) 2006.6《程序员》笔记
8. Action过滤
Na57 · 2008-05-06 · via 博客园 - Na57

Action过滤

原文: http://quickstarts.asp.net/3-5-extensions/mvc/ActionFiltering.aspx

1. 介绍
一般Action与用户动作是一对一的关系,用户的某一个动作对应一个特定的Action.
然而,有时你可能会希望在Action执行的前后执行指定的操作,MVC中可以通过Action过滤器来实现这个功能.
Action过滤器是一个自定义的Attribute,它的应用范围包括:日志,身份认证和授权,输出缓存,Web抓取过滤,本地化,动态Action等.

2. 实现一个Action过滤器
Action过滤器必须派生自ActionFilterAttribute抽象类,并实现OnActionExecutingOnActionExecuted两个方法。一个示例如下:

public class LoggingFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(FilterExecutingContext filterContext)
{
filterContext.HttpContext.Trace.Write("Starting: " +
filterContext.ActionMethod.Name);
}

public override void

OnActionExecuted(FilterExecutedContext filterContext)
{
if (filterContext.Exception != null)
{
filterContext.HttpContext.Trace.Write("Exception thrown");
}
}
}

3. Action过滤器上下文
过滤器上下文包括FilterExecutingContext和FilterExecutedContext两个,他们都派生自FilterContext类,提供ActionMethod属性,用于获取Action方法的信息。
FilterExecutingContext包含一个Cancel属性,允许你取消正在执行的Action。
FilterExecutedContent包含ExceptionExceptionHandled属性。若Action执行期间没有错误发生,则Exception属性为null,若Exception属性不为null,且其他过滤器已经处理过这个异常,则ExceptionHandled属性的值为true。

4. 在控制器中使用过滤器
给需要使用过滤器的Action方法加上想要的过滤器Attribute即可。如:

public class HomeController : Controller
{
[LoggingFilter]
public void Index()
{
RenderView("Index");
}
}

5. 实现“控制器级”的过滤器
可能出现一种情况:控制器中的所有Action方法都需要同一个过滤器。这是可以通过重写控制器类的OnActionExecutingOnActionExecuted方法来实现过滤器。

6. Action过滤器的范围
过滤器是可继承的。

7. 过滤器的执行顺序
过滤器提供了Order顺序来指定多个过滤器的执行顺序。Order的值必须为0或正数,若Order为-1,则表明不指定该过滤器的执行顺序,它将在所有被指定顺序的过滤器执行之后执行。另外,若两个过滤器的Order值相同将会产生异常。一个例子如下:

[Filter1(Order = 2)]
[Filter2(Order = 3)]
[Filter3(Order = 1)]
public void Index()
{
RenderView("Index");
}

另外,过滤器的两个方法以及“控制器级”的过滤器的两个方法之间的执行顺序也有规定,详见原文。

8. 过滤器执行顺序的例子