zoukankan      html  css  js  c++  java
  • 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

    做笔记使用

  • 相关阅读:
    RIP2与OSPFv2 动态路由协议区别
    Linux平台下SSD的TRIM指令的最佳使用方式(不区别对待NVMe)
    MLNX网卡驱动安装
    字符串/字符数组读入(char/string)
    【NOIP2016模拟3】图书列表
    活动选择-贪心
    数列极差问题-STL优先队列-贪心
    货物搬运-贪心
    【NOIP 2002提高】均分纸牌-贪心
    【HAOI2008】糖果传递-贪心
  • 原文地址:https://www.cnblogs.com/NotEnough/p/6866577.html
Copyright © 2011-2022 走看看