Control中的AOP实现非业务需求
一、能够使用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
做笔记使用
Control中的AOP实现非业务需求的更多相关文章
- MVC之Control中使用AOP
原文转载自http://www.cnblogs.com/iamlilinfeng/archive/2013/03/02/2940162.html 本文目标 一.能够使用Control中的AOP实现非业 ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- .Net中的AOP系列之构建一个汽车租赁应用
返回<.Net中的AOP>系列学习总目录 本篇目录 开始一个新项目 没有AOP的生活 变更的代价 使用AOP重构 本系列的源码本人已托管于Coding上:点击查看. 本系列的实验环境:VS ...
- .Net中的AOP读书笔记系列之AOP介绍
返回<.Net中的AOP>系列学习总目录 本篇目录 AOP是什么? Hello,World! 小结 本系列的源码本人已托管于Coding上:点击查看,想要注册Coding的可以点击该连接注 ...
- .Net中的AOP系列之《拦截位置》
返回<.Net中的AOP>系列学习总目录 本篇目录 位置拦截 .Net中的字段和属性 PostSharp位置拦截 真实案例--懒加载 .Net中的懒加载 使用AOP实现懒加载 如何懒加载字 ...
- .Net中的AOP系列之《方法执行前后——边界切面》
返回<.Net中的AOP>系列学习总目录 本篇目录 边界切面 PostSharp方法边界 方法边界 VS 方法拦截 ASP.NET HttpModule边界 真实案例--检查是否为移动端用 ...
- .Net中的AOP系列之《间接调用——拦截方法》
返回<.Net中的AOP>系列学习总目录 本篇目录 方法拦截 PostSharp方法拦截 Castle DynamicProxy方法拦截 现实案例--数据事务 现实案例--线程 .Net线 ...
- 转-Spring Framework中的AOP之around通知
Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...
- 【转】在.Net中关于AOP的实现
原文地址:http://www.uml.org.cn/net/201004213.asp 一.AOP实现初步 AOP将软件系统分为两个部分:核心关注点和横切关注点.核心关注点更多的是Domain Lo ...
随机推荐
- eclipse 通过maven 开发storm项目
window系统安装java 首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/ind ...
- poj2392磊石头——排序后背包
题目: 首先按限制高度从小到大排序,不会影响可行解,而不排序可能卡掉正确的情况: 用%2滚动数组时一定注意每次复制上一种情况,因为这个WA了好几次. 代码如下: #include<iostrea ...
- POJ3352(连通分量缩点)
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10352 Accepted: 514 ...
- jmeter 开发自己的java请求 二次开发
package test; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient. ...
- JAVA基础知识 String s = new String("ABC") VS String s = "abc"
一: String s = new String("ABC") VS String s = "abc" String s = "abc&q ...
- 关于UML图的生成
想把一个java工程生成UML图非常简单,之前我的eclipse是4.2.0的,没有对应的GEF,所以我索性就直接把工程粘到了My Eclipse中,因为My Eclipse里面有UML自动生成的功能 ...
- php + mssql乱码
当用PHP自带的模块php_mssql.dll去调用MSSQL数据库时,中文数据会乱码.但如果我们采用ADODB的方式去做,就不会乱码了.请看下面的具体实例: 调用开源的adodb.inc.php(支 ...
- go实现冒泡排序和快速排序
项目结构 冒泡排序算法,源文件bubblesort.go package bubblesort // 冒泡排序 func BubbleSort(values []int) { for i := 0; ...
- SQL 分组获取产品 前两条记录
select * from ( select *, ROW_NUMBER() over(partition by IPAddress order by recordtime desc) as rowN ...
- luogu 1169 棋盘制作(单调栈/悬线)
luogu 1169 棋盘制作(单调栈/悬线) 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应 ...