Control中的AOP实现非业务需求

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

做笔记使用

时间: 2024-08-29 17:18:47

Control中的AOP实现非业务需求的相关文章

MVC之Control中使用AOP

原文转载自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 // Sum

.Net语言中关于AOP 的实现详解

来源: IT人家  发布时间: 2011-03-22 20:28  阅读: 3546 次  推荐: 2   原文链接   [收藏] 摘要:该文章主要和大家讲解开发应用系统时在.Net语言中关于AOP 的实现. 文章主要和大家讲解开发应用系统时在.Net语言中关于AOP 的实现.LogAspect完成的功能主要是将Advice与业务对象的方法建立映射,并将其添加到Advice集合中.由于我们在AOP实现中,利用了xml配置文件来配置PointCut,因此对于所有Aspect而言,这些操作都是相同的

ASP.NET MVC中有关AOP的编程

AOP(Aspect oriented programming)面向切面编程,主要意思是把相同.相似的并且零散的逻辑抽离出来,统一处理,这样不仅维护起来方便,也使得代码更加关注自己本身,清晰明了. 比如我们常见的权限检查,验证登陆,异常处理等都是散乱在系统各个地方,比如管理员在登陆状态才可以添加一个学生信息: public ActionResult AddStudent(Student student) { if (currentUser != null) { StudentDAL.Add(st

转-Spring Framework中的AOP之around通知

Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: springaop设计模式beanintegerclass 2011-12-07 11:39 6108人阅读 评论(0) 收藏 举报 在第一部分,您看到了如何使用Spring AOP来实现跟踪和记录方面.跟踪和记录都是"消极"方面,因为它们的出现并不会对应用程序的其他行为产生影响.它们都使用了消极的b

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

原文地址:http://www.uml.org.cn/net/201004213.asp 一.AOP实现初步 AOP将软件系统分为两个部分:核心关注点和横切关注点.核心关注点更多的是Domain Logic,关注的是系统核心的业务:而横切关注点虽与核心的业务实现无关,但它却是一种更Common的业务,各个关注点离散地分布于核心业务的多处.这意味着,如果不应用AOP,那么这些横切关注点所代表的业务代码,就会分散在系统各处,导致系统中的每个模块都与这些业务具有很强的依赖性.在这里,所谓横切关注点所代

spring中的aop注解(整合junit测试)

使用spring中的aop前先来了解一下spring中aop中的一些名词 Joimpoint(连接点):目标对象中,所有可能增强的方法 PointCut(切入点):目标对象,已经增强的方法 Advice(通知/增强):增强的代码 Target(目标对象):被代理对象 Weaving(织入):将通知应用到切入点的过程 Proxy(代理):将通知织入到目标对象之后,形成代理对象 aspect(切面):切入点+通知 一:不使用spring的aop注解 以javaEE中的service层为例 UserS

利用多态,实现一般处理程序(ashx)中的AOP(切面编程)

本文是对工作中的项目进行代码优化(完善登陆验证的AOP切面编程)时,所遇到的各种解决方案思考过程. 项目背景:由ashx+nvelocity构建的简单B/S问卷系统,现需要优化登录验证环节(时隔若干个月在回顾代码果然是一个痛苦的过程~) nvelocity是velocity框架针对.net的版本,核心是拼html字符串后返回客户端,与MVC的前后端代码隔离有异曲同工之妙.加之一般处理程序ashx不需要像asp.net那样走生成控件树的过程,执行上更是省时省力.故简单系统用ashx+nveloci

Spring中的AOP(五)——在Advice方法中获取目标方法的参数

摘要: 本文介绍使用Spring AOP编程中,在增强处理方法中获取目标方法的参数,定义切点表达式时使用args来快速获取目标方法的参数. 获取目标方法的信息 访问目标方法最简单的做法是定义增强处理方法时,将第一个参数定义为JoinPoint类型,当该增强处理方法被调用时,该JoinPoint参数就代表了织入增强处理的连接点.JoinPoint里包含了如下几个常用的方法: Object[] getArgs:返回目标方法的参数 Signature getSignature:返回目标方法的签名 Ob

Net中的AOP

.Net中的AOP系列之<单元测试切面> 返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle DynamicProxy测试 测试一个拦截器 注入依赖 PostSharp测试 对PostSharp切面进行单元测试 注入依赖 PostSharp和测试的问题 小结 本节的源码本人已托管于Coding上:点击查看. 本系列的实验环境:VS 2013 Update 5(建议最好使用集成了Nuget的VS版本,VS