Control中的AOP实现非业务需求的功能

本文目标

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

posted @ 2019-07-10 09:21  进步者One  阅读(111)  评论(0编辑  收藏  举报