一、能够使用Control中的AOP实现非业务需求的功能

本文目录

一、ActionFilterAttribute类

二、实现自定义Attribute

一、ActionFilterAttribute类

Action筛选条件的基类

 1 using System;
2
3 namespace System.Web.Mvc
4 {
5 // Summary:
6 // Represents the base class for filter attributes.
7 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
8 public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
9 {
10 // Summary:
11 // Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class.
12 protected ActionFilterAttribute();
13
14 // Summary:
15 // Called by the ASP.NET MVC framework after the action method executes.
16 //
17 // Parameters:
18 // filterContext:
19 // The filter context.
20 public virtual void OnActionExecuted(ActionExecutedContext filterContext);
21 //
22 // Summary:
23 // Called by the ASP.NET MVC framework before the action method executes.
24 //
25 // Parameters:
26 // filterContext:
27 // The filter context.
28 public virtual void OnActionExecuting(ActionExecutingContext filterContext);
29 //
30 // Summary:
31 // Called by the ASP.NET MVC framework after the action result executes.
32 //
33 // Parameters:
34 // filterContext:
35 // The filter context.
36 public virtual void OnResultExecuted(ResultExecutedContext filterContext);
37 //
38 // Summary:
39 // Called by the ASP.NET MVC framework before the action result executes.
40 //
41 // Parameters:
42 // filterContext:
43 // The filter context.
44 public virtual void OnResultExecuting(ResultExecutingContext filterContext);
45 }
46 }

OnActionExecuting:在Action执行之前执行该方法

OnActionExecuted:在Action执行之后执行该方法

OnResultExecuting:在Result执行之前执行该方法

OnResultExecuted:在Result执行之后执行该方法

二、实现自定义Attribute

在MVC框架基础上实现自定义Attribute只需实现ActionFilterAttribute中的虚方法即可

1.代码

 1 using System.Web.Mvc;
2
3 namespace MVC3.Demo.App_Code
4 {
5 public class LogActionFilter : ActionFilterAttribute
6 {
7 public string LogMessage { get; set; }
8
9 public override void OnActionExecuting(ActionExecutingContext filterContext)
10 {
11 filterContext.HttpContext.Response.Write(@"在Action执行之前执行" + LogMessage + "<br />");
12 base.OnActionExecuting(filterContext);
13 }
14
15 public override void OnActionExecuted(ActionExecutedContext filterContext)
16 {
17 filterContext.HttpContext.Response.Write(@"在Action执行之后执行" + LogMessage + "<br />");
18 base.OnActionExecuted(filterContext);
19 }
20
21 public override void OnResultExecuting(ResultExecutingContext filterContext)
22 {
23 filterContext.HttpContext.Response.Write(@"在Result执行之前执行" + LogMessage + "<br />");
24 base.OnResultExecuting(filterContext);
25 }
26
27 public override void OnResultExecuted(ResultExecutedContext filterContext)
28 {
29 filterContext.HttpContext.Response.Write(@"在Result执行之后执行" + LogMessage + "<br />");
30 base.OnResultExecuted(filterContext);
31 }
32 }
33 }

2.使用

1         [LogActionFilter(LogMessage = "日志写入:Validation方法")]
2 public ActionResult Validation()
3 {
4 return View();
5 }

3.效果

版权:http://www.cnblogs.com/iamlilinfeng

做笔记使用

最新文章

  1. ECF R9(632E) &amp; FFT
  2. angular监听
  3. 4、界面前端设计师要阅读的书籍 - IT软件人员书籍系列文章
  4. Windows 8.1/Server 2012 R2/Embedded 8.1 with Update 3(MSDN最新版)
  5. hihoCoder #1033 : 交错和 (数位Dp)
  6. MVC返回http状态码
  7. readonly 与 const
  8. 兼容IE与firefox、chrome的css 线性渐变(linear-gradient)
  9. object-c 内存管理机制的学习
  10. centos安装docker容器
  11. Unity热更新之C#反射加载程序集
  12. 为什么不能在 body 标签的前面的 script 标签中定义 JS 全局变量?
  13. get 乱码解决方案
  14. c#操作数据库的增删改查语句及DataGridView简单使用
  15. MaxiSYS Elite
  16. MySQL用户及权限管理
  17. Windows-universal-samples学习笔记系列二:Controls, layout, and text
  18. python 类 __module__ __class__
  19. Sqlite - constraint failed[0x1555]: UNIQUE constraint failed
  20. [Java]如何为一个自定义类型的List排序。

热门文章

  1. 1107 Social Clusters (30)(30 分)
  2. Java命名规范(简略)
  3. 【Lintcode】137.Clone Graph
  4. rman理论(一)
  5. KCF+Opencv3.0+Cmake+Win10 测试
  6. css3 实现运动动画 圆与椭圆
  7. Linux&amp;nbsp;shell脚本全面学习
  8. plsql&amp;nbsp;分页
  9. login.aspx.cs
  10. IOSerialize,xml和json,soap序列化器,二进制序列化器,XML序列化器,文件 检查、新增、复制、移动、删除