一直都知道MVC中的ActionFilter,只是没有在实际项目中使用过。

顾名思义,ActionFilter就是指在Action上的Filter, 那么,在Action上的Filter到底有哪些呢。首先我们看看MVC中的Controller基类(抽象类)

  public abstract class Controller : ControllerBase, IActionFilter, IAuthenticationFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IController, IAsyncManagerContainer
{
}

我们可以看到,它有5个Filter,分别是IActionFilter, IAuthenticationFilter, IAuthorizationFilter, IExceptionFilter, IResultFilter. 我们先看看第一个IActionFilter

IActionFilter解析

从名称就可以看出,IActionFilter是一个接口.源码如下
    //
// 摘要:
// Defines the methods that are used in an action filter.
public interface IActionFilter
{
//
// 摘要:
// Called after the action method executes.
//
// 参数:
// filterContext:
// The filter context.
void OnActionExecuted(ActionExecutedContext filterContext);
//
// 摘要:
// Called before an action method executes.
//
// 参数:
// filterContext:
// The filter context.
void OnActionExecuting(ActionExecutingContext filterContext);
}

可以看到,该接口中有两个方法,一个是OnActionExecuted (执行完Action之后执行该方法),另一个是OnActionExecuting(执行Action之前执行该方法)

也就是这两个方法分别在Action的前后执行. 所以它可以用于记录日志,获取action的访问量......

知道了之后,那么我们如何来实现ActionFilter呢。

我们注意到,Controller基类继承了IActionFilter接口,而我们在MVC中写的Controller又都继承自Controller基类, 所以我们应该可以直接在MVC的Controller中override这两个方法

1. 用override的方式来实现ActionFilter

      public class HomeController : Controller
{
public ActionResult Index()
{
return View();
} protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("Execute something before action"); base.OnActionExecuting(filterContext);
} protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("Execute something after action"); base.OnActionExecuted(filterContext);
}
}

这样好像就可以的,但是它有个问题,就是它只能用于HomeController, 有没有一种方法,我能写一个ActionFilter,可以用于多个Controller中的不同Action呢

显然,可以做到。我们可以自己写一个ActionFilter的类,使它继承IActionFilter, 如下:

2. 自定义一个ActionFilter 属性来实现IActionFilter接口

     public class MyActionFilterAttribute : Attribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("Execute something before action");
} public void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("Execute something after action");
}
}

好了,到现在我们就有自己写的ActionFilter了,叫做“MyActionFilter”, 如何使用它呢,很简单的

     public class HomeController : Controller
{
[MyActionFilter]
public ActionResult Index()
{
return View();
}
}

除了上面两种方法,还有没有其他方法呢。事实上是有的,MVC Framework自己给我们提供了一个ActionFilterAttribute特性,这也是一个抽象类,我们来看看源码

      //
// 摘要:
// Represents the base class for filter attributes.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
{
//
// 摘要:
// Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class.
protected ActionFilterAttribute(); //
// 摘要:
// Called by the ASP.NET MVC framework after the action method executes.
//
// 参数:
// filterContext:
// The filter context.
public virtual void OnActionExecuted(ActionExecutedContext filterContext);
//
// 摘要:
// Called by the ASP.NET MVC framework before the action method executes.
//
// 参数:
// filterContext:
// The filter context.
public virtual void OnActionExecuting(ActionExecutingContext filterContext);
//
// 摘要:
// Called by the ASP.NET MVC framework after the action result executes.
//
// 参数:
// filterContext:
// The filter context.
public virtual void OnResultExecuted(ResultExecutedContext filterContext);
//
// 摘要:
// Called by the ASP.NET MVC framework before the action result executes.
//
// 参数:
// filterContext:
// The filter context.
public virtual void OnResultExecuting(ResultExecutingContext filterContext);
}

所以,我们可以写一个自己的ActionFilter属性,来实现MVC Framework提供的这个类ActionFilterAttribute就好

3. 自定义一个ActionFilter 类来继承ActionFilterAttribute类
namespace MyApplication.Controllers
{
public class HomeController : Controller
{
[MyActionFilter]
public ActionResult Index()
{
return View();
}
} public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
} public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
}
}
以上参考 http://www.cnblogs.com/huangxincheng/p/5671106.html

ASP.NET MVC中的ActionFilter介绍学习的更多相关文章

  1. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  2. Ext.Net学习笔记24:在ASP.NET MVC中使用Ext.Net

    在前面的笔记中已经介绍了如何在ASP.NET WebForm中使用Ext.Net,由于这个系列一直在WebForm中使用,所以并没有涉及到ASP.NET MVC中的用法. 如果你要在ASP.NET M ...

  3. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  4. 在 ASP.NET MVC 中使用 HTTPS (SSL/TLS) -- 学习

    在 ASP.NET MVC 中使用 HTTPS (SSL/TLS) IS 7如何实现http重定向https HTTPS 升级指南

  5. <转>ASP.NET学习笔记之在ASP.NET MVC中使用DropDownList

    看到一篇关于dropdownlist的用法很好的阐述,比较清楚,留着,防止以后自己不记得,还可以瞅瞅. 在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便 ...

  6. ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用

    Ajax的全名为:Asynchronous Javascript And XML(异步 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发技术.Ajax技术首先向Web服务器发送 ...

  7. [转]ASP.NET MVC中你必须知道的13个扩展点

    本文转自:http://www.cnblogs.com/ejiyuan/archive/2010/03/09/1681442.html ScottGu在其最新的博文中推荐了Simone Chiaret ...

  8. ASP.NET MVC中你必须知道的13个扩展点

         ScottGu在其最新的博文中推荐了Simone Chiaretta的文章13 ASP.NET MVC extensibility points you have to know,该文章为我 ...

  9. 如何在 ASP.NET MVC 中集成 AngularJS(3)

    今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...

随机推荐

  1. 第七篇、os、sys、random、time、datetime、logging

    一.sys 用于提供对Python解释器相关的操作: 1 2 3 4 5 6 7 8 9 sys.argv           命令行参数List,第一个元素是程序本身路径 sys.exit(n)   ...

  2. MySQL服务器调优思路

    1.mysqladmin -uroot ext|awk '/Queries/{q=$4}/Threads_connected/{c=$4}/Threads_running/{r=$4}END{prin ...

  3. Shiro-权限认证(授权)-编程式授权

    权限认证 权限认证也就是访问控制,即在应用中控制谁能访问哪些资源 权限认证核心要素 权限 : 即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利 角色 : 是权限的 ...

  4. Ogre场景编辑器Ogitor源代码的构建

    本文转自:http://blog.csdn.net/zhengkangchen/article/details/6000769 Ogitor-0.4.2源代码构建,不少时间,这里记录一下: 下载源代码 ...

  5. OTSU大津法对图像二值化

    OTSU算法 (1)原理: 对于图像I(x,y),前景(即目标)和背景的分割阈值记作T,属于背景的像素个数占整幅图像的比例记为ω0,其平均灰度μ0:前景像素个数占整幅图像的比例为ω1,其平均灰度为μ1 ...

  6. apache 简单笔记

    1.端口监听 可以多个 Listen 80Listen 8080 2.多网站 Listen 80Listen 8080 # Virtual hostsInclude conf/extra/httpd- ...

  7. Unity3D之Mesh(六)绘制扇形、扇面、环形

    前言: 绘制了圆,就想到绘制与之相关的几何图形,以便更灵活的掌握Mesh动态创建模型的机制与方法. 一.分析: 首先,结合绘制圆的过程绘制环形: 圆形是由segments个等腰三角形组成的(上一篇中, ...

  8. spring-springMVC-MyBatis整合配置文件

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  9. codeforces 612D The Union of k-Segments (线段排序)

    D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  10. linux命令学习笔记(48):watch命令

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测 一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下 ...