一、能够使用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实现非业务需求的更多相关文章

  1. MVC之Control中使用AOP

    原文转载自http://www.cnblogs.com/iamlilinfeng/archive/2013/03/02/2940162.html 本文目标 一.能够使用Control中的AOP实现非业 ...

  2. Spring中的AOP

    什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...

  3. .Net中的AOP系列之构建一个汽车租赁应用

    返回<.Net中的AOP>系列学习总目录 本篇目录 开始一个新项目 没有AOP的生活 变更的代价 使用AOP重构 本系列的源码本人已托管于Coding上:点击查看. 本系列的实验环境:VS ...

  4. .Net中的AOP读书笔记系列之AOP介绍

    返回<.Net中的AOP>系列学习总目录 本篇目录 AOP是什么? Hello,World! 小结 本系列的源码本人已托管于Coding上:点击查看,想要注册Coding的可以点击该连接注 ...

  5. .Net中的AOP系列之《拦截位置》

    返回<.Net中的AOP>系列学习总目录 本篇目录 位置拦截 .Net中的字段和属性 PostSharp位置拦截 真实案例--懒加载 .Net中的懒加载 使用AOP实现懒加载 如何懒加载字 ...

  6. .Net中的AOP系列之《方法执行前后——边界切面》

    返回<.Net中的AOP>系列学习总目录 本篇目录 边界切面 PostSharp方法边界 方法边界 VS 方法拦截 ASP.NET HttpModule边界 真实案例--检查是否为移动端用 ...

  7. .Net中的AOP系列之《间接调用——拦截方法》

    返回<.Net中的AOP>系列学习总目录 本篇目录 方法拦截 PostSharp方法拦截 Castle DynamicProxy方法拦截 现实案例--数据事务 现实案例--线程 .Net线 ...

  8. 转-Spring Framework中的AOP之around通知

    Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...

  9. 【转】在.Net中关于AOP的实现

    原文地址:http://www.uml.org.cn/net/201004213.asp 一.AOP实现初步 AOP将软件系统分为两个部分:核心关注点和横切关注点.核心关注点更多的是Domain Lo ...

随机推荐

  1. BZOJ2726:任务安排(DP+斜率优化+二分)

    机器上有N个需要处理的任务,它们构成了一个序列.这些任务被标号为1到N,因此序列的排列为1,2,3...N.这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务 ...

  2. Codeforces Round #397 题解

    Problem A. Neverending competitions 题目大意 一个团队有多个比赛,每次去比赛都会先订机票去比赛地点,然后再订机票返回.给出\(n\)个含有起止地点的购票记录(不按时 ...

  3. finetune

    微调的具体方法和技巧有很多种,这里总结了在不同场景下的微调技巧: 1)新数据集比较小且和原数据集相似.因为新数据集比较小(比如<5000),如果fine-tune可能会过拟合:又因为新旧数据集类 ...

  4. scala & spark实战

    java.lang.Long is not a valid external type for schema of string   java.lang.RuntimeException: Error ...

  5. Enum定义位域, 即可以通过位操作来产生未命名的值

    通过FlagsAttribute可以实现. // A bit field or flag enumeration of harvesting seasons. [Flags] public enum ...

  6. 云-资讯-Micron-Insight:云的形成方式 — 以及它的发展方向

    ylbtech-云-资讯-Micron-Insight:云的形成方式 — 以及它的发展方向 1.返回顶部 1. 云的形成方式 — 以及它的发展方向 当你坐下来开始一天工作的时候,你可能不会考虑到你所做 ...

  7. Swoole 协程与 Go 协程的区别

    Swoole 协程与 Go 协程的区别 进程.线程.协程的概念 进程是什么? 进程就是应用程序的启动实例. 例如:打开一个软件,就是开启了一个进程. 进程拥有代码和打开的文件资源,数据资源,独立的内存 ...

  8. Django 之 requirement.txt 依赖文件生成

    通过依赖文件,别人在使用我们的项目时,不需要再一个个去安装所需模块,只需安装依赖文件即可. 1. 导出整个虚拟环境依赖 # 在项目根目录中,打开终端执行以下命令 # 生成 requirements.t ...

  9. 获得用户IP、城市、国家等信息的api接口

    1 这个信息比较多 https://api.ipdata.co/?api-key=test <script> $.get("https://api.ipdata.co?api-k ...

  10. linux端口netstat

    netstat -aptn命令行,查看所有开启的端口号 netstat -nupl       查看所有udp端口号 netstat -ntpl   查看所有tcp端口号 查看某服务占用的端口情况,比 ...