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 ...
随机推荐
- linux crontab 定时任务解析
-----------crontab定时任务---------------------- 检查crontab工具是否安装 crontab -l 检查crontab服务是否启动 service cron ...
- 利用树的先序和后序遍历打印 os 中的目录树
[0]README 0.1)本代码均为原创,旨在将树的遍历应用一下下以加深印象而已:(回答了学习树的遍历到底有什么用的问题?)你对比下linux 中的文件树 和我的打印结果就明理了: 0.2)我们采用 ...
- Log4j2升级jar包冲突问题
升级Log4j2后日志提示jar包冲突: SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar ...
- iOS UIWebview添加请求头的两种方式
1.在UIWebviewDelegate的方法中拦截request,设置request的请求头,废话不多说看代码: - (BOOL)webView:(UIWebView *)webView shoul ...
- mysql系列之5.mysql备份恢复
备份数据: mysqldump #mysqldump -uroot -p123456 test > /test_bak.sql #egrep -v "#|\*|--|^$" ...
- slide.js
define(['jquery'], function (jquery) { function buildSmooth(config, motivateCallBack) { var timer = ...
- wxPython的Refresh与事件双重响应
#!/usr/bin/env python import wx class DoubleEventFrame(wx.Frame): def __init__(self, parent, id): wx ...
- FastJson处理Map List 对象
Fastjson是一个Java语言编写的高性能功能完善的JSON库. Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发. 1.遵循http://json.org标准,为其官 ...
- 创建spring管理的自定义注解
转自: http://blog.csdn.net/wuqiqing_1/article/details/52763372 Annotation其实是一种接口.通过Java的反射机制相关的API来访问A ...
- API的理解和使用——列表类型的命令
列表类型的命令及对应的时间复杂度 操作 命令 功能 时间复杂度 添加 rpush key value [value ...] 向右插入 O(k),k是元素个数 lpush key value [val ...