原文转载自http://www.cnblogs.com/iamlilinfeng/archive/2013/03/02/2940162.html

本文目标

一、能够使用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.效果

MVC之Control中使用AOP的更多相关文章

  1. Control中的AOP实现非业务需求

    一.能够使用Control中的AOP实现非业务需求的功能 本文目录 一.ActionFilterAttribute类 二.实现自定义Attribute 一.ActionFilterAttribute类 ...

  2. spring aop在mvc的controller中加入切面无效

    spring aop在mvc的controller中加入切面无效 因为MVC的controller,aop默认使用jdk代理.要使用cglib代理. 在spring-mybatis.xml配置文件中加 ...

  3. Spring MVC 中使用AOP 进行统一日志管理--注解实现

    1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...

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

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

  5. .Net中的AOP系列之《单元测试切面》

    返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle DynamicProxy测试 测试一个拦截器 注入依赖 ...

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

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

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

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

  8. MVC:Control与View传值

    MVC页面传值的方式主要有三种: 第一种: 采用ViewData.采用键值对的方式,ViewData存储的是一个object类型,传到view层需要强类型转换:使用起来类似于字典集合模式: ViewD ...

  9. Net中的AOP

    .Net中的AOP系列之<单元测试切面>   返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle ...

随机推荐

  1. 实现RTSP网站微信直播方案EasyNVR(linux版)部署问题之:ERR_CONTENT_LENGTH_MISMATCH

    发现问题: 想要优化一下EasyNVR相关功能,内部测试软件,于是在linux系统中部署了一台EasyNVR.当部署好,运行起来发现问题: EasyNVR的配置页面数据出不来. 分析问题: 基于是we ...

  2. AWS:2.根设备类型、EC2生命周期状态、User Data

    主要内容 1.根设备类型 linux: /dev/sda1 windows: 系统盘 2.实例生命周期 生命周期状态:停止.终止.重启 3.用户数据(UserData) 实例在初始化,运行之前给定的用 ...

  3. python连接redis并插入url

    #!/usr/bin/env python # -*- coding:utf8 -*- import redis ''' 这种连接是连接一次就断了,耗资源.端口默认6379,就不用写 r = redi ...

  4. AFNetworking3.0使用简介

    注意到咱们集成的版本为3.0.4. 下面的类已从AFNetworking 3.0中废弃: AFURLConnectionOperation AFHTTPRequestOperation AFHTTPR ...

  5. lk进kernel

    -- ] [upmu_is_chr_det] [] DRAM Rank : [] DRAM Rank[] Start = 0x40000000, Size = 0x25fc0000 [] DRAM R ...

  6. Java基础:hashCode与equals个人学习记录

    摘要: 本文主要记录本人对hashCode和对equals两个知识点的学习过程. 从学生时期初学java,就知道hashCode和equals这两个方法,工作中equals方法使用也是特别频繁,要说e ...

  7. Linux-3.14.12内存管理笔记【kmalloc与kfree实现】【转】

    本文转载自:http://blog.chinaunix.net/uid-26859697-id-5573776.html kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少 ...

  8. Window7 环境下 MariaDB 的安装 及使用

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方 ...

  9. Uncaught TypeError: Illegal invocation解决

    jquery中报了这个错,仔细一看,有个使用ajax的地方,其中有个参数是从页面某个文本框获取的,本应该 $('#id').value ,被我写成了 $('id') .所以报错,目前已解决.

  10. win8+sdk8+vs2012+freeglut+glew开发opengl

    写给想要学习opengl的同学们. 刚开始学习opengl的时候,对于整个环境的搭建以及一些概念不太清晰,网上的资料又比较凌乱,因此在此总结一下,方便大家. 首先,是有一个windows系统,我用的是 ...