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 ...
随机推荐
- css 跳转电脑分辨率
因为我们经常在项目中要适配各种屏幕,为了方便前端的开发和测试.我们可以直接把电脑的分辨率调整到需要适配的最小的分辨率,其实还有一种更直接粗暴的方法.直接按F12打开控制台,在收拉浏览器就能看到目前的分 ...
- wpf datagrid row的命中测试
1. 添加鼠标左键处理 AddHandler(DataGrid.MouseLeftButtonDownEvent, new RoutedEventHandler(grdStudyList_MouseL ...
- Mysql常用命令行大全(三)
/**操作数据库*/ SHOW DATABASES; CREATE DATABASE db; SHOW DATABASES; DROP DATABASE db; /**操作表*/ USE db; S ...
- windows平台下新网络库RIO ( Winsock high-speed networking Registered I/O)
What's New for Windows Sockets Microsoft Windows 8 and Windows Server 2012 introduce new Windows Soc ...
- Java中的数组和方法
3.1 数组的定义和使用 数组(Array)是用来存储一组相同数据类型数据的集合.数组中的每个数据称为一个元素(element),数组可以分为一维数组,二维数组和多维数组.我们 主要讲解一维数组和二维 ...
- selenium上传文件,怎么操作
#通过os.path.abspath()方法,打开图片的绝对路径,然后,定位上传按钮,然后,send_keys()方法中,添加这个文件路径就可以了
- Windows下搭建Subversion 服务器
一.准备工作 1.获取 Subversion 服务器程序 到官方网站(http://subversion.tigris.org/)下载最新的服务器安装程序.目前最新的是1.5版本,具体下载地址在:ht ...
- E20190420-hm
impact n. 巨大影响; 强大作用; 撞击; 冲撞; 冲击力; v. (对某事物) 有影响,有作用; 冲击; 撞击; incident n. 发生的事情(尤指不寻常的或讨厌的); 严 ...
- Python:asyncio模块学习
python asyncio 网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程.无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态 ...
- redis win连接以及配置连接密码
redis连接格式为 redis-cli -h host -p port -a password 但由于刚安装的redis是没有密码的 因此可以进行直接连接, cd转到redis目录里 redis-c ...