这首诗他们早已开始看呵呵ActionFilter,从英文名字上只不过就约莫晓得ActionFilter是Action上的Filter,对吧,因此Action上的Filter约莫有两个呢???

那个难题只不过却是蛮单纯的,即使他们听闻Mvc这类是三个可扩展性很强的架构,大自然是一层层有截击,一层层有过滤器,对吧,比如说他们看见的如下表所示Controller类。

public abstract class Controller : ControllerBase, IActionFilter, IAuthenticationFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IController, IAsyncManagerContainer
{
}

从那个派生类的Controller中,他们就能看见有5个Filter,如:IActionFilter,IAuthenticationFilter,IAuthorizationFilter,IExceptionFilter,IResultFilter,

对吧,具体来说他们却是从第三个ActionFilter讲起。

一:IActionFilter导出

那时他们晓得IActionFilter是三个USB,接下去钟爱的是那个ActionFilter里头究竟是甚么,比如说我下面截屏的这种。

//
// 全文:
// Defines the methods that are used in an action filter.
public interface IActionFilter
{
//
// 全文:
// Called after the action method executes.
//
// 模块:
// filterContext:
// The filter context.
void OnActionExecuted(ActionExecutedContext filterContext);
//
// 全文:
// Called before an action method executes.
//
// 模块:
// filterContext:
// The filter context.
void OnActionExecuting(ActionExecutingContext filterContext);
}

从下面这段标识符中,他们能看见只不过那个USB里头多于三个形式,三个是OnActionExecuting,三个是OnActionExecuted,看这英文名字你如果就知道

只不过是在Action的其间依次继续执行,对吧,那如此一来,精明的你就想不到了应用领域情景,历史记录笔记,以获取action的用户数量,以方便快捷先期收费项目~~~ 接下去我

们来看一看是不是来同时实现这三个形式。

1. 用override的形式同时实现ActionFilter

那时我们都晓得Controller类早已同时实现了那个USB,那他们他们的XXXController正好又承继了那个父Controller,因此直面此种情形,他们能用

override来同时实现,比如说下面我同时实现的这种。

1 public class HomeController : Controller
2 {
3 public ActionResult Index
4 {
5 return View;
6 }
7
8 protected override void OnActionExecuting(ActionExecutingContext filterContext)
9 {
10 filterContext.HttpContext.Response.Write(“hello”);
11
12 base.OnActionExecuting(filterContext);
13 }
14
15 protected override void OnActionExecuted(ActionExecutedContext filterContext)
16 {
17 filterContext.HttpContext.Response.Write(“world”);
18
19 base.OnActionExecuted(filterContext);
20 }
21 }

就这种他们就轻松加愉快的同时实现了,是不是很单纯,但是仔细一想,这种的形式却是有一点限制的,也是说他们override的依赖性太强了,比如说说多于

class extends IActionFilter才能,接下去他们再看有没有更灵活的形式来同时实现。

2. 自定义class extends IActionFilter

要想做到高度的灵活性,他们必须将那个同时实现类做成三个原子单位,有了那个原子单位,他们就能很方便快捷的将那个不可拆解的原子性应用领域到各个地方

去,对吧,那个原子在C中能用Attribute来同时实现,比如说下面这种:

1 public class MyActionFilterAttribute : Attribute, IActionFilter
2 {
3 public void OnActionExecuted(ActionExecutedContext filterContext)
4 {
5 filterContext.HttpContext.Response.Write(“hello”);
6 }
7
8 public void OnActionExecuting(ActionExecutingContext filterContext)
9 {
10 filterContext.HttpContext.Response.Write(“world”);
11 }
12 }

ok,那时他们早已得到了三个原子性质的MyActionFilterAttribute特性,接下去他们能将那个MyActionFilterAttribute应用领域到任何地方,如下表所示图:

1 public class HomeController : Controller
2 {
3 [MyActionFilter]
4 public ActionResult Index
5 {
6 return View;
7 }
8 }

3. ActionFilterAttribute

除了他们同时实现以下Attribute特性和IActionFilterUSB,他们还能承继三个mvc架构提供给他们的ActionFilterAttribute特性,迫不及待的看呵呵吧~

1 //
2 // 全文:
3 // Represents the base class for filter attributes.
4 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
5 public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
6 {
7 //
8 // 全文:
9 // Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class.
10 protected ActionFilterAttribute;
11
12 //
13 // 全文:
14 // Called by the ASP.NET MVC framework after the action method executes.
15 //
16 // 模块:
17 // filterContext:
18 // The filter context.
19 public virtual void OnActionExecuted(ActionExecutedContext filterContext);
20 //
21 // 全文:
22 // Called by the ASP.NET MVC framework before the action method executes.
23 //
24 // 模块:
25 // filterContext:
26 // The filter context.
27 public virtual void OnActionExecuting(ActionExecutingContext filterContext);
28 //
29 // 全文:
30 // Called by the ASP.NET MVC framework after the action result executes.
31 //
32 // 模块:
33 // filterContext:
34 // The filter context.
35 public virtual void OnResultExecuted(ResultExecutedContext filterContext);
36 //
37 // 全文:
38 // Called by the ASP.NET MVC framework before the action result executes.
39 //
40 // 模块:
41 // filterContext:
42 // The filter context.
43 public virtual void OnResultExecuting(ResultExecutingContext filterContext);
44 }

从那个Attribute中能看见,它整合了IActionFilter, IResultFilter,大自然就有了这三个USB的形式,好了,不多说,他们来同时实现呵呵那个抽象类吧。

namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
[MyActionFilter]
public ActionResult Index
{
return View;
}
}

public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
}
}

二:源标识符预测

最好的源标识符预测形式,肯定是希望你下载三个reflector插件,这种他们就能以获取到运行时的可调试标识符以及能看见的调用堆栈,尤其是调用堆

栈,对你来说非常的重要。

1. 具体来说他们下三个断点在 OnActionExecuting形式里头,如下表所示图:

自动草稿

2. 通过调用堆栈回退到上三个堆栈,如图:

自动草稿

那个形式只不过非常的有意思,从形式名称中能看见,只不过它是三个递归的模式,也是OnActionExecuting” =

1.本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2.分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3.不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4.本站提供的源码、模板、插件等其他资源,都不包含技术服务请大家谅解!
5.如有链接无法下载或失效,请联系管理员处理!
6.本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!