MVC 过滤器 ActionFilterAttribute

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using 过滤器.Controllers;



namespace 过滤器.Filtes

{

    public class MyActionFiterAttribute : ActionFilterAttribute

    {



        /// <summary>

        /// Action过滤器 执行方法之前

        /// </summary>

        /// <param name="filterContext"></param>

        public override void OnActionExecuting(ActionExecutingContext filterContext)

        {

            //RouteData保存了当前请求的路由信息

            // filterContext.RouteData.Values["action"];

            // filterContext.RouteData.DataTokens["area"]; 获取区域名称

            if (filterContext.ActionDescriptor.IsDefined(typeof(MyActionFiterAttribute), false))//检查当前请求的action是否有贴MyActionFiterAttribute标签

            {

                //不执行Action方法,也不执行OnActionExecuted方法,会执行Result过滤器

                filterContext.Result = new ContentResult();//给当前请求设置返回结果,不会再继续执行当前的action

            }

            filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnActionExecuting");

            base.OnActionExecuting(filterContext);

        }

        //执行方法之后

        public override void OnActionExecuted(ActionExecutedContext filterContext)

        {

            filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnActionExecuted");

            base.OnActionExecuted(filterContext);

        }

        //Result过滤器

        public override void OnResultExecuted(ResultExecutedContext filterContext)

        {

            filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnResultExecuted");

            base.OnResultExecuted(filterContext);

        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)

        {

            filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnResultExecuting");

            base.OnResultExecuting(filterContext);

        }

    }

}

 

你可能感兴趣的:(attribute)