原文转载自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的更多相关文章

  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. iOS - web自适应宽高(预设置的大小)

    //web自适应宽高 -(void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"wessd"); [ webView s ...

  2. 矩阵十题【六】 poj3070 Fibonacci

    题目链接:http://poj.org/problem? id=3070 题目大意:给定n和10000,求第n个Fibonacci数mod 10000 的值,n不超过2^31. 结果保留四位数字. 非 ...

  3. Unix环境高级编程—进程关系

    终端登录 网络登录 进程组 getpgrp(void) setpgid(pid_t pid, pid_) 会话: 是一个或多个进程组的集合,通常由shell的管道将几个进程编成一组. setsid(v ...

  4. 流畅python学习笔记第十八章:使用asyncio包处理并发(二)

    前面介绍了asyncio的用法.下面我们来看下如何用协程的方式来实现之前的旋转指针的方法 @asyncio.coroutine def spin(msg): write,flush=sys.stdou ...

  5. SAP增强 和VA01相关增强点介绍

    -转 sap寻找用户出口方法 sap的用户出口总共有三代: 一.User EXIT 第一代的用户出口,它们include在SAP标准程序的源代码里,可以说他们是源代码的一部分,你改了这种出口就相当于改 ...

  6. Java8系列之重新认识HashMap(转)

    转自美团电瓶技术团队:原文地址 简介 Java为数据结构中的映射定义了一个接口java.util.Map,此接口主要有四个常用的实现类,分别是HashMap.Hashtable.LinkedHashM ...

  7. Java基础之I/O流

    一.数据流的基本概念 数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流.数据写入程序可以是一段.一段地向数据流管道中写入数据, ...

  8. Java for LeetCode 083 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  9. [2018-11-03]2018年10月28日宁波dotnet社区活动回顾及下次活动预告

    离上次活动,有半年了,汗.之后尽量保证每月一次,以组织为主,多邀请嘉宾来分享. 本次活动不足之处 人手不足:由于活动组织事项受限于人手(目前就我一个,这次活动前后我又应邀给大红鹰学院应届生介绍dotn ...

  10. 微信小程序上传项目到github

    Step1:点击使用版本控制 点击开发工具右上角的版本控制按钮 Step2:点击初始化本地仓库: 等待初始化结束 Step3:在github新建仓库 Step4: 配置git用户密码或ssh: Ste ...