一、能够使用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. 集训Day1

    雅礼集训2017Day1的题 感觉上不可做实际上还挺简单的吧 T1 区间加 区间除法向下取整 查询区间和 区间最小值 大力上线段树,把除法标记推到底,加法标记就是按照线段树的来 先拿30 然后60的数 ...

  2. AIM Tech Round 4 (Div. 2)

    A题 分析:暴力 #include "iostream" #include "cstdio" #include "cstring" #inc ...

  3. Tensorflow知识点学习

    1.TensorFlow中Tensor维度理解: (1)对于2维Tensor 0维对应列 1维对应行 (2)维度操作举例: 对于k维的,tf.reduce_sum(x, axis=k-1)的结果是对最 ...

  4. ubuntu_deb安装命令

    dpkg命令常用格式如下: sudo dpkg -I iptux.deb#查看iptux.deb软件包的详细信息,包括软件名称.版本以及大小等(其中-I等价于--info) sudo dpkg -c ...

  5. 利用d3js绘出环形百分比环

    利用d3js绘出环形百分比环 (function() { var numberData = [{ value : 0.334, text : "33.4%", color : &q ...

  6. 数据库路由中间件MyCat - 使用篇(2)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 基本概念 3. 分片 3.1 分片节点(dataNode) 表被水平切分后,每个分片表所在的数据库就是一个分 ...

  7. php 连接 memcached 并调用

    话不多说,上代码,自己看注释 <?php header("Content-type: text/html; charset=utf-8"); $mem = new Memca ...

  8. AI决策算法 之 GOAP (二)

    http://blog.csdn.net/lovethRain/article/details/67634803 GOAP 的主要逻辑: 1.Agent的状态机初始化Idle状态 2.Idel状态根据 ...

  9. IT 面试题

    1.JDK1.X新增的功能   ==>>> 2.字符流和字节流的区别,使用场景,相关类   ==>>> 字节流与字符流 在Java.io包中操作文件内容的主要有两大 ...

  10. Google Analytics & Webtrend 使用笔记

    GA和webtrend中的参数,每每遇到了都记不住.遇到一点记一点好了... Page Views(PV): 浏览次数.用户每打开一个页面,记录为1个PV:用户多次打开同一个页面,PV累计多次. Da ...