MVC之Control中使用AOP
原文转载自http://www.cnblogs.com/iamlilinfeng/archive/2013/03/02/2940162.html
本文目标
一、能够使用Control中的AOP实现非业务需求的功能
本文目录
一、ActionFilterAttribute类
二、实现自定义Attribute
一、ActionFilterAttribute类
Action筛选条件的基类
- using System;
- namespace System.Web.Mvc
- {
- // Summary:
- // Represents the base class for filter attributes.
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
- public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
- {
- // Summary:
- // Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class.
- protected ActionFilterAttribute();
- // Summary:
- // Called by the ASP.NET MVC framework after the action method executes.
- //
- // Parameters:
- // filterContext:
- // The filter context.
- public virtual void OnActionExecuted(ActionExecutedContext filterContext);
- //
- // Summary:
- // Called by the ASP.NET MVC framework before the action method executes.
- //
- // Parameters:
- // filterContext:
- // The filter context.
- public virtual void OnActionExecuting(ActionExecutingContext filterContext);
- //
- // Summary:
- // Called by the ASP.NET MVC framework after the action result executes.
- //
- // Parameters:
- // filterContext:
- // The filter context.
- public virtual void OnResultExecuted(ResultExecutedContext filterContext);
- //
- // Summary:
- // Called by the ASP.NET MVC framework before the action result executes.
- //
- // Parameters:
- // filterContext:
- // The filter context.
- public virtual void OnResultExecuting(ResultExecutingContext filterContext);
- }
- }
OnActionExecuting:在Action执行之前执行该方法
OnActionExecuted:在Action执行之后执行该方法
OnResultExecuting:在Result执行之前执行该方法
OnResultExecuted:在Result执行之后执行该方法
二、实现自定义Attribute
在MVC框架基础上实现自定义Attribute只需实现ActionFilterAttribute中的虚方法即可
1.代码
- using System.Web.Mvc;
- namespace MVC3.Demo.App_Code
- {
- public class LogActionFilter : ActionFilterAttribute
- {
- public string LogMessage { get; set; }
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- filterContext.HttpContext.Response.Write(@"在Action执行之前执行" + LogMessage + "<br />");
- base.OnActionExecuting(filterContext);
- }
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- filterContext.HttpContext.Response.Write(@"在Action执行之后执行" + LogMessage + "<br />");
- base.OnActionExecuted(filterContext);
- }
- public override void OnResultExecuting(ResultExecutingContext filterContext)
- {
- filterContext.HttpContext.Response.Write(@"在Result执行之前执行" + LogMessage + "<br />");
- base.OnResultExecuting(filterContext);
- }
- public override void OnResultExecuted(ResultExecutedContext filterContext)
- {
- filterContext.HttpContext.Response.Write(@"在Result执行之后执行" + LogMessage + "<br />");
- base.OnResultExecuted(filterContext);
- }
- }
- }
2.使用
- [LogActionFilter(LogMessage = "日志写入:Validation方法")]
- public ActionResult Validation()
- {
- return View();
- }
3.效果
MVC之Control中使用AOP的更多相关文章
- Control中的AOP实现非业务需求
一.能够使用Control中的AOP实现非业务需求的功能 本文目录 一.ActionFilterAttribute类 二.实现自定义Attribute 一.ActionFilterAttribute类 ...
- spring aop在mvc的controller中加入切面无效
spring aop在mvc的controller中加入切面无效 因为MVC的controller,aop默认使用jdk代理.要使用cglib代理. 在spring-mybatis.xml配置文件中加 ...
- Spring MVC 中使用AOP 进行统一日志管理--注解实现
1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...
- .Net中的AOP读书笔记系列之AOP介绍
返回<.Net中的AOP>系列学习总目录 本篇目录 AOP是什么? Hello,World! 小结 本系列的源码本人已托管于Coding上:点击查看,想要注册Coding的可以点击该连接注 ...
- .Net中的AOP系列之《单元测试切面》
返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle DynamicProxy测试 测试一个拦截器 注入依赖 ...
- .Net中的AOP系列之《方法执行前后——边界切面》
返回<.Net中的AOP>系列学习总目录 本篇目录 边界切面 PostSharp方法边界 方法边界 VS 方法拦截 ASP.NET HttpModule边界 真实案例--检查是否为移动端用 ...
- 转-Spring Framework中的AOP之around通知
Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...
- MVC:Control与View传值
MVC页面传值的方式主要有三种: 第一种: 采用ViewData.采用键值对的方式,ViewData存储的是一个object类型,传到view层需要强类型转换:使用起来类似于字典集合模式: ViewD ...
- Net中的AOP
.Net中的AOP系列之<单元测试切面> 返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle ...
随机推荐
- 实现RTSP网站微信直播方案EasyNVR(linux版)部署问题之:ERR_CONTENT_LENGTH_MISMATCH
发现问题: 想要优化一下EasyNVR相关功能,内部测试软件,于是在linux系统中部署了一台EasyNVR.当部署好,运行起来发现问题: EasyNVR的配置页面数据出不来. 分析问题: 基于是we ...
- AWS:2.根设备类型、EC2生命周期状态、User Data
主要内容 1.根设备类型 linux: /dev/sda1 windows: 系统盘 2.实例生命周期 生命周期状态:停止.终止.重启 3.用户数据(UserData) 实例在初始化,运行之前给定的用 ...
- python连接redis并插入url
#!/usr/bin/env python # -*- coding:utf8 -*- import redis ''' 这种连接是连接一次就断了,耗资源.端口默认6379,就不用写 r = redi ...
- AFNetworking3.0使用简介
注意到咱们集成的版本为3.0.4. 下面的类已从AFNetworking 3.0中废弃: AFURLConnectionOperation AFHTTPRequestOperation AFHTTPR ...
- lk进kernel
-- ] [upmu_is_chr_det] [] DRAM Rank : [] DRAM Rank[] Start = 0x40000000, Size = 0x25fc0000 [] DRAM R ...
- Java基础:hashCode与equals个人学习记录
摘要: 本文主要记录本人对hashCode和对equals两个知识点的学习过程. 从学生时期初学java,就知道hashCode和equals这两个方法,工作中equals方法使用也是特别频繁,要说e ...
- Linux-3.14.12内存管理笔记【kmalloc与kfree实现】【转】
本文转载自:http://blog.chinaunix.net/uid-26859697-id-5573776.html kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少 ...
- Window7 环境下 MariaDB 的安装 及使用
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方 ...
- Uncaught TypeError: Illegal invocation解决
jquery中报了这个错,仔细一看,有个使用ajax的地方,其中有个参数是从页面某个文本框获取的,本应该 $('#id').value ,被我写成了 $('id') .所以报错,目前已解决.
- win8+sdk8+vs2012+freeglut+glew开发opengl
写给想要学习opengl的同学们. 刚开始学习opengl的时候,对于整个环境的搭建以及一些概念不太清晰,网上的资料又比较凌乱,因此在此总结一下,方便大家. 首先,是有一个windows系统,我用的是 ...