原文转载自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.效果

最新文章

  1. [连载]《C#通讯(串口和网络)框架的设计与实现》- 14.序列号的设计,不重复的实现一机一码
  2. Appium简单测试用例
  3. [翻译]Telnet简单介绍及在windows 7中开启Telnet客户端
  4. Ubuntu14.04或16.04下Hadoop及Spark的开发配置
  5. jQuery 教程
  6. ReactiveCocoa学习
  7. Android JavaMail
  8. ASPose导出excel简单操作
  9. isnull的使用方法
  10. 通过HTTP头控制浏览器的缓存
  11. jQuery RemoveAttr(checked)之后再Attr(checked)属性无效果的原因分析
  12. vbs运行批处理
  13. expect实现ssh自动登录
  14. c++设计模式15 --组合模式
  15. 高可用开源方案 Keepalived VS Heartbeat对比
  16. Python中数学函数
  17. Springboot 5.Springboot 返回cookies信息的post接口开发
  18. 003-单例OR工厂模式
  19. Django-----加入MD5格式上传图片
  20. Twitter开发

热门文章

  1. maven项目The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java Build Path
  2. 03 redis之string类型命令解析
  3. 多媒体开发之---live555的多线程支持,原本只是单线程,单通道
  4. 【BZOJ1499】[NOI2005]瑰丽华尔兹 单调队列+DP
  5. 【BZOJ1969】[Ahoi2005]LANE 航线规划 离线+树链剖分+线段树
  6. protobuf json xml比较
  7. 云服务器 ECS Linux CentOS 修改内核引导顺序
  8. 解决Ubuntu(乌班图)vi/vim模式下粘贴的代码内容会多出的空格的问题
  9. BZOJ3878: [Ahoi2014&amp;Jsoi2014]奇怪的计算器
  10. 用php動態產生各種尺寸的圖片