無廢話MVC入門教程七[Control中使用AOP]


朋友炒股兩個月賺了10萬,我幫他推廣一下公眾號,把錢用來投資總比放銀行連通貨膨脹都跑不過里強硬核離職,在家炒股 ,這是他每天的日志,有些經驗是花錢也買不到的。

本文目標

一、能夠使用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM