无废话MVC入门教程七[Control中使用AOP]
2013-03-02 21:56
453 查看
mvc技术交流,欢迎加群:
![](http://pub.idqqimg.com/wpa/images/group.png)
群号:529597507
二、实现自定义Attribute
OnActionExecuting:在Action执行之前执行该方法
OnActionExecuted:在Action执行之后执行该方法
OnResultExecuting:在Result执行之前执行该方法
OnResultExecuted:在Result执行之后执行该方法
2.使用
3.效果
![](http://images.cnitblog.com/blog/414533/201303/02215125-641f251a326a44e6aacd8c046012b750.x-png)
![](http://pub.idqqimg.com/wpa/images/group.png)
群号:529597507
本文目标
一、能够使用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.效果
版权:http://www.cnblogs.com/iamlilinfeng
相关文章推荐
- 无废话MVC入门教程八[MvcPager分页控件的使用]
- 无废话MVC入门教程四[视图中的Layout使用]
- 无废话MVC入门教程四[视图中的Layout使用]
- 无废话MVC入门教程五[Control与View交互]
- 无废话MVC入门教程五[Control与View交互]
- 无废话MVC入门教程四[视图中的Layout使用]
- 无废话MVC入门教程四[视图中的Layout使用]
- MVC入门教程八[MvcPager分页控件的使用]
- 无废话MVC入门教程六[Model中的验证规则]
- MVC中使用SignaIR入门教程
- [置顶] MVC中使用signalR入门教程
- 无废话MVC入门教程二
- [置顶] MVC中使用signalR入门教程
- MVC入门教程-视图中的Layout使用
- 无废话MVC入门教程九[实战一:用户注册与登陆]
- 无废话MVC入门教程一[概述、环境安装、创建项目]
- 无废话MVC入门教程三[路由设置及视图入门]
- 无废话MVC入门教程一[概述、环境安装、创建项目]
- 无废话MVC入门教程二[第一个小Demo]
- 无废话MVC入门教程二[第一个小Demo]