zoukankan      html  css  js  c++  java
  • 过滤器

      过滤器为了把附加逻辑附加到MVC框架的请求。

    一、过滤器总类

    过滤器类型 接口 默认实现 描述
    认证过滤器 IAuthenticationFilter N/A 最先运行,在任何其他过滤器或动作方法之前,但是在授权过滤器之后可以再次运行。
    授权过滤器 IAuthorizationFilter AuthorizeAttribute 在认证后,其他过滤器或动作前,第二个运行。
    动作过滤器 IActionFilter ActionFilterAttribute 在动作方法之前及之后运行
    结果过滤器 IResultFilter ActionFilterAttribute 在动作结果被执行之前和之后运行
    异常过滤器 IExceptionFilter HandleErrorAttribute 仅在另一个过滤器、动作方法或者动作结果抛出异常时运行。

    二、过滤器接口分析

      认证过滤器IAuthenticationFilter,是MVC5新特性,接口定义如下:  

    namespace System.Web.Mvc.Filters
    {
        public interface IAuthenticationFilter
        {
            void OnAuthentication(AuthenticationContext filterContext);
            void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);
        }
    }
    View Code

      

      授权过滤器IAuthorizationFilter,默认实现AuthorizeAttribute.先看IAuthorizationFilter接口定义。   

    // 摘要: 
        //     定义授权筛选器所需的方法。
        public interface IAuthorizationFilter
        {
            // 摘要: 
            //     在需要授权时调用。
            //
            // 参数: 
            //   filterContext:
            //     筛选器上下文。
            void OnAuthorization(AuthorizationContext filterContext);
        }
    View Code

      授权过滤接口只定义了OnAuthorization方法,默认实现AuthorizeAttribute可以通过Public属性Users和Roles来控制授权策略。

      

      动作过滤器IActionFilter,默认实现ActionFilterAttribute,接口定义:  

    namespace System.Web.Mvc
    {
        // 摘要: 
        //     定义操作筛选器中使用的方法。
        public interface IActionFilter
        {
            // 摘要: 
            //     在执行操作方法后调用。
            //
            // 参数: 
            //   filterContext:
            //     筛选器上下文。
            void OnActionExecuted(ActionExecutedContext filterContext);
            //
            // 摘要: 
            //     在执行操作方法之前调用。
            //
            // 参数: 
            //   filterContext:
            //     筛选器上下文。
            void OnActionExecuting(ActionExecutingContext filterContext);
        }
    }
    View Code

      接口方法OnActionExecuting在动作执行之前调用。OnActionExecuted方法在动作执行之后调用。

      结果过滤器IResultFilter接口定义:

    namespace System.Web.Mvc
    {
        // 摘要: 
        //     定义结果筛选器所需的方法。
        public interface IResultFilter
        {
            // 摘要: 
            //     在操作结果执行后调用。
            //
            // 参数: 
            //   filterContext:
            //     筛选器上下文。
            void OnResultExecuted(ResultExecutedContext filterContext);
            //
            // 摘要: 
            //     在操作结果执行之前调用。
            //
            // 参数: 
            //   filterContext:
            //     筛选器上下文。
            void OnResultExecuting(ResultExecutingContext filterContext);
        }
    }
    View Code

      接口方法OnResultExecuting在结果执行之前调用,OnResultExecuted方法在结果执行之后调用。

      异常过滤器IExceptionFilter,默认实现HandleErrorAttribute,接口定义如下:  

    namespace System.Web.Mvc
    {
        // 摘要: 
        //     定义异常筛选器所需的方法。
        public interface IExceptionFilter
        {
            // 摘要: 
            //     在发生异常时调用。
            //
            // 参数: 
            //   filterContext:
            //     筛选器上下文。
            void OnException(ExceptionContext filterContext);
        }
    }
    View Code

      异常过滤器定义了OnException方法。异常过滤器使用后要所ExceptionHandled设置为True已处理,否则MVC框架会将异常视为未处理继续外抛。

  • 相关阅读:
    后向边
    图的割点、桥和双连通分支的基本概念
    Hihocoder 1062 最近公共祖先1
    会场问题 差分解法
    POJ2976 01分数规划 普通题
    Hihocoder 1049
    hihocoder 1050树中最长路
    Hihocoder 1055
    POJ1463
    C语言|博课作业02
  • 原文地址:https://www.cnblogs.com/bro-ma/p/7087988.html
Copyright © 2011-2022 走看看