先来看看一个例子演示过滤器有什么用:

public class AdminController : Controller {
// ... instance variables and constructor
public ViewResult Index() {
if (!Request.IsAuthenticated) {
FormsAuthentication.RedirectToLoginPage();
}
// ...rest of action method
}
public ViewResult Create() {
if (!Request.IsAuthenticated) {
FormsAuthentication.RedirectToLoginPage();
}
// ...rest of action method
}
...

AdminController控制器的众多Action中我们都需要判定当前验证用户,这里有很多重复的代码,我们可以简化为:

[Authorize]
public class AdminController : Controller {
// ... instance variables and constructor
public ViewResult Index() {
// ...rest of action method
}
public ViewResult Create() {
// ...rest of action method
}
...

Authorize特性类AuthorizeAttribute就称作MVC的Filter,它在横向为MVC框架扩展功能,让我们可以更方便的处理日志、授权、缓存等而不影响纵向主体功能。

MVC常用的滤器类型:

  • Authorization:实现IAuthorizationFilter接口,默认实现类AuthorizeAttribute,在调用Action方法前首先处理认证信息。
  • Action:实现IActionFilter接口,默认实现类ActionFilterAttribute,在运行Action前后调用实现一些额外的动作。
  • Result:实现IResultFilter接口,默认实现类ActionFilterAttribute,在action result运行前后调用实现额外的动作。
  • Exception:实现IExceptionFilter接口,默认实现类HandleErrorAttribute,仅在其他过滤器或者action方法或者action result抛出异常时调用。

过滤器可以应用在整个控制器类上,也可以单对某个Action方法,当然也可以是同时应用多个过滤器:

[Authorize(Roles="trader")] // applies to all actions
public class ExampleController : Controller {
[ShowMessage] // applies to just this action
[OutputCache(Duration=60)] // applies to just this action
public ActionResult Index() {
// ... action method body
}
}

Authorization过滤器

Authorization过滤器用于控制仅授权的用户可以调用某个Action方法,它必须实现IAuthorizationFilter接口:

namespace System.Web.Mvc {
public interface IAuthorizationFilter {
void OnAuthorization(AuthorizationContext filterContext);
}
}

处理安全方面的代码必须谨慎全面,一般我们不要直接实现接口IAuthorizationFilter,而从AuthorizeAttribute扩展自定义类:

public class CustomAuthAttribute : AuthorizeAttribute {
  private bool localAllowed;
  public CustomAuthAttribute(bool allowedParam) {
    localAllowed = allowedParam;
  }
  protected override bool AuthorizeCore(HttpContextBase httpContext) {
    if (httpContext.Request.IsLocal) {
    return localAllowed;
    } else {
    return true;
    }
  }
}

这里定义了CustomAuthAttribute过滤器,如果请求来自于非本机总是允许,如果是本机请求则视传入参数allowedParam而定:

public class HomeController : Controller {
  [CustomAuth(false)]
  public string Index() {
    return "This is the Index action on the Home controller";
  }
}

默认实现AuthorizeAttribute足够应付多数授权功能限制,我们可以传入参数限制访问的用户或者角色:

[Authorize(Users = "adam, steve, jacqui", Roles = "admin")] 

需要注意的是AuthorizeAttribute仅仅负责授权,而用户的认证则依赖于ASP.NET的认证机制。

Exception过滤器

Exception过滤器需要实现IExceptionFilter接口:

namespace System.Web.Mvc {
public interface IExceptionFilter {
void OnException(ExceptionContext filterContext);
}
}

从filterContext我们可以获取很多相关信息,比如Controller、HttpContext、IsChildAction、 RouteData、Result、Exception、ExceptionHandled等。异常的具体信息我们可以从Exception获取,我们设 置ExceptionHandled为true报告异常已经处理,在设置为true之前最好检查异常是否已经被action方法上其他的过滤器处理,以避 免重复的错误纠正动作。如果异常未被处理(ExceptionHandled!=true),MVC框架使用默认的异常处理程序显示ASP.NET的黄屏 错误页面。

我们可以创建自定义的异常过滤器:

 public class RangeExceptionAttribute : FilterAttribute, IExceptionFilter {

        public void OnException(ExceptionContext filterContext) {

            if (!filterContext.ExceptionHandled &&
filterContext.Exception is ArgumentOutOfRangeException) { int val = (int)(((ArgumentOutOfRangeException)filterContext.Exception).ActualValue); //filterContext.Result = new RedirectResult("~/Content/RangeErrorPage.html");
filterContext.Result = new ViewResult {
ViewName = "RangeError",
ViewData = new ViewDataDictionary<int>(val)
};
filterContext.ExceptionHandled = true;
}
}
}

注意这里RangeExceptionAttribute除了实现IExceptionFilter接口还从FilterAttribute继承, 这是因为MVC的过滤器类还必须实现IMvcFilter接口,这里我们没有直接实现IMvcFilter接口,改为从默认封装类 FilterAttribute继承。如下使用这个自定义过滤器:

...
[RangeException]
public string RangeTest(int id) {
  if (id > 100) {
  return String.Format("The id value is: {0}", id);
  } else {
  throw new ArgumentOutOfRangeException("id");
  }
}
...

在发生异常时过滤器会将我们重定向到RangeError视图。

通常我们不需要创建自己的异常处理器,而使用MVC提供的默认过滤器HandleErrorAttribute:

[HandleError(ExceptionType = typeof(ArgumentOutOfRangeException), View = "RangeError",Master="")]

属性ExceptionType指定要处理的异常类型,如果不指定则处理所有异常类型;view指定错误时要渲染的视;master指定所用的页面布局。

在使用HandleErrorAttribute过滤前我们必须在Web.config的<System.Web>一节启用CusomErrors:

<customErrors mode="On" defaultRedirect="/Content/RangeErrorPage.html"/> 

默认customErros为RemoteOnly,defaultRedirect指定一个默认的错误页面。

Action过滤器

Action过滤器必须实现IActionFilter接口:

namespace System.Web.Mvc {
public interface IActionFilter {
  void OnActionExecuting(ActionExecutingContext filterContext);
  void OnActionExecuted(ActionExecutedContext filterContext);
  }
}

OnActionExecuting()在调用action方法前调用,OnActionExecuted()则在调用action方法后调用。

OnActionExecuting的一个实现例子:

public class CustomActionAttribute : FilterAttribute, IActionFilter {

        public void OnActionExecuting(ActionExecutingContext filterContext) {
if (filterContext.HttpContext.Request.IsLocal) {
filterContext.Result = new HttpNotFoundResult();
}
} public void OnActionExecuted(ActionExecutedContext filterContext) {
// not yet implemented
}
}

这里我们仅实现了OnActionExecuting方法,在从本地请求任何应用此过滤器的action方法时返回404错误。

OnActionExecuted的一个例子:

 public class ProfileActionAttribute : FilterAttribute, IActionFilter {
private Stopwatch timer; public void OnActionExecuting(ActionExecutingContext filterContext) {
timer = Stopwatch.StartNew();
} public void OnActionExecuted(ActionExecutedContext filterContext) {
timer.Stop();
if (filterContext.Exception == null) {
filterContext.HttpContext.Response.Write(
string.Format("<div>profile result method elapsed time: {0}</div>",
timer.Elapsed.TotalSeconds));
}
}
}

在调用action方法前我们启动计时器,在action方法调用后停止计时器并输出计时器所计action方法运行时长。

Result过滤器

Result过滤器实现IResultFilter接口:

namespace System.Web.Mvc {
public interface IResultFilter {
void OnResultExecuting(ResultExecutingContext filterContext);
void OnResultExecuted(ResultExecutedContext filterContext);
}
}

Result过滤器操作的是action方法返回结果,有意思的是即使action方法返回void所应用的Result过滤器也会动作。

类似上面的ProfileAction过滤器我们可以对Result运行计时:

public class ProfileResultAttribute : FilterAttribute, IResultFilter {
private Stopwatch timer; public void OnResultExecuting(ResultExecutingContext filterContext) {
timer = Stopwatch.StartNew();
} public void OnResultExecuted(ResultExecutedContext filterContext) {
timer.Stop();
filterContext.HttpContext.Response.Write(
string.Format("<div>Result elapsed time: {0}</div>",
timer.Elapsed.TotalSeconds));
}
}

内建的Result过滤器类ActionFilterAttribute

MVC为我们提供了ActionFilterAttribute类,它同时实现了action和result过滤器接口:

public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter,
IResultFilter{
public virtual void OnActionExecuting(ActionExecutingContext filterContext) {
}
public virtual void OnActionExecuted(ActionExecutedContext filterContext) {
}
public virtual void OnResultExecuting(ResultExecutingContext filterContext) {
}
public virtual void OnResultExecuted(ResultExecutedContext filterContext) {
}
}
}

我们只需要继承该类并重载需要的方法,比如:

public class ProfileAllAttribute : ActionFilterAttribute {
private Stopwatch timer; public override void OnActionExecuting(ActionExecutingContext filterContext) {
timer = Stopwatch.StartNew();
} public override void OnResultExecuted(ResultExecutedContext filterContext) {
timer.Stop();
filterContext.HttpContext.Response.Write(
string.Format("<div>Total elapsed time: {0}</div>",
timer.Elapsed.TotalSeconds));
}
}

Controller类的过滤器支持

MVC的Controller类内部实现了IAuthorizationFilter、IActionFilter、IResultFilter、IExceptionFilte四个接口,并提供OnXXX的虚函数供调用,比如:

    public class HomeController : Controller {
private Stopwatch timer; protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
timer = Stopwatch.StartNew();
} protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
timer.Stop();
filterContext.HttpContext.Response.Write(
string.Format("<div>Total elapsed time: {0}</div>",
timer.Elapsed.TotalSeconds));
} ...

全局过滤器

全局过滤器应用于应用程序内所有控制器的所有action方法,我们在App_Start/FilterConfig.cs可以注册全局过滤器:

public class FilterConfig {
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new HandleErrorAttribute());
filters.Add(new ProfileAllAttribute());
}
}

HandleErrorAttribute是VS为我们默认添加的,用于未处理异常错误时显示/Views/Shared /Error.cshtml页面;ProfileAllAttribute则是我们添加的自定义过滤器,运行任何action方法时都会调用这个过滤器。

其他MVC内建过滤器

MVC框架还内建提供以下过滤器:

  • RequireHttps:指示必须以HTTPS访问action方法,仅用于HTTP get方法
  • OutputCache:缓存action方法的结果
  • ValidateInput和ValidationAntiForgeryToken:安全授权相关的过滤器
  • AsnycTimeOut和NoAsyncTimeout:用于异步控制器
  • ChildActionOnlyAttribute:用于授权Html.Action或者Html.RenderAction调用子action方法

这些过滤器的使用方法可以参见MSDN。

过滤器的运行顺序

过滤器的运行按先后顺序是:authorization过滤器、action过滤器、result过滤器,期间任何时刻发生未处理异常调用异常处理 器。这是针对不同类型的过滤器,但是如果所应用的是同一类的过滤器呢?MVC默认并不保证同类型过滤器的调用程序,也就是说很可能并非按照出现在代码中的 先后程序来调用过滤器,但是我们可以显式的指定它们的调用顺序:

...
[SimpleMessage(Message="A", Order=2)]
[SimpleMessage(Message="B", Order=1)]
public ActionResult Index() {
Response.Write("Action method is running");
return View();
}
...

这里SimpleMessage是一个action过滤器,通过order我们指定它们的运行顺序: B->OnActionExecuting、A->OnActionExecuting、A->OnActionExecuted、 B->OnActionExecuted。注意A的OnActionExecuted先于B的OnActionExecuted,和 OnActionExecuting正好相反,这是无法改变的。在不指定order时,MVC内部指定为-1。另外如果同类型过滤器指定相同的order 比如都是1,还要根据在哪里应用的过滤器来分先后执行:首先执行的是全局过滤器、然后是控制器类上、最后才是action方法;例外的是 exception过滤器,它的顺序正好与此相反!

ASP.NET MVC 4 (三) 过滤器的更多相关文章

  1. ASP.NET MVC 视图(三)

    ASP.NET MVC 视图(三) 前言 上篇对于Razor视图引擎和视图的类型做了大概的讲解,想必大家对视图的本身也有所了解,本篇将利用IoC框架对视图的实现进行依赖注入,在此过程过会让大家更了解的 ...

  2. ASP.NET MVC 路由(三)

    ASP.NET MVC路由(三) 前言 通过前两篇的学习会对路由系统会有一个初步的了解,并且对路由系统中的Url规则有个简单的了解,在大家的脑海中也有个印象了,那么路由系统在ASP.NETMVC中所处 ...

  3. Asp.Net MVC<五>:过滤器

    ControllerActionInvoker在执行过程中除了利用ActionDescriptor完成对目标Action方法本身的执行外,还会执行相关过滤器(Filter).过滤器采用AOP的设计,它 ...

  4. ASP.NET MVC学习之过滤器篇(2)

    下面我们继续之前的ASP.NET MVC学习之过滤器篇(1)进行学习. 3.动作过滤器 顾名思义,这个过滤器就是在动作方法调用前与调用后响应的.我们可以在调用前更改实际调用的动作,也可以在动作调用完成 ...

  5. asp.net MVC之 自定义过滤器(Filter) - shuaixf

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...

  6. asp.net MVC之 自定义过滤器(Filter)

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  7. ASP.NET MVC学习之过滤器篇(1)

    一.前言 继前面四篇ASP.NET MVC的随笔,我们继续向下学习.上一节我们学习了关于控制器的使用,本节我们将要学习如何使用过滤器控制用户访问页面. 二.正文 以下的示例建立在ASP.NET MVC ...

  8. ASP.NET MVC 第三回 Controller与View

    这节我们让ASP.NET MVC真正的跑起来 一.新建Controller 首先我们自己新建一个新的Controller在Controllers上点右键,添加,Controller选项   之后出现一 ...

  9. 笨鸟先飞之ASP.NET MVC系列之过滤器(01过滤器简介)

    过滤器 什么是过滤器? 过滤器(Filter) 主要的作用大致可以理解为把我们的附加逻辑注入到MVC框架的请求处理. 在ASP.NET MVC的请求处理中一种有19个管道事件分别是 BeginRequ ...

随机推荐

  1. jQuery学习笔记(二)jQuery中DOM操作

    目录 DOM操作分类 jQuery中的各种DOM操作 查找节点 创建节点 删除节点 复制节点 替换节点 包裹节点 属性操作 样式操作 对HTML.文本和值的操作 遍历节点 CSS-DOM操作 小结 本 ...

  2. IE8兼容性经验小结

    DOCTYPE 首先需要确保HTML页面开始部分要有DOCTYPE声明.DOCTYPE告诉浏览器使用什么样的HTML或XHTML规范来解析HTML文档,具体会影响: 1.对标记,attributes, ...

  3. Web String path问题

    request.getContextPath()"下方出现了红色的波浪线,提示的错误信息是 "The method getContextPath() from the type H ...

  4. javax.crypto.BadPaddingException: Given final block not properly padded

    一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...

  5. SQL-Server使用点滴(三)

    除了基本的数据库,数据表,数据记录操作之外,SQL-Server还为我们提供了比较丰富的其他对象元素.函数,过程,触发器,序列,映射服务器, 以及对各种元素的系统表信息读取与判断. --先加一个利用递 ...

  6. service&thread

    service:在后台静默运行.服务必须在一个线程中才能运行.长时间的在后台运行,而且不需要交互的情况下,使用服务. thread:在后台运行,但是可以与用户进行相应的交互操作.比如说,音乐播放器,后 ...

  7. 读《编写可维护的JavaScript》第六章总结

    第六章 避免使用全局变量 JavaScript执行环境在很多方面都有其独特之处,全局变量就是其中之一.“全局变量”是一个神秘的对象,它表示了脚本的最外层上下文. 在浏览器中,windows对象往往重载 ...

  8. Linux连接Internet

    Linux连接Internet 1. Linux网卡 1.1 查看内核所扑捉到的网卡信息(已经安装到服务器并且被Linux内核识别的网卡) [root@www ~]# dmesg | grep -in ...

  9. MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

  10. union内嵌struct用法

      // union内嵌struct用法 // 众所周知,union为联合体,struct为结构体.下面根据实例谈谈用法   #include <stdio.h>   #include & ...