Asp.net Mvc过滤器获取请求方法自定义特性

来源:互联网 发布:java完全自学宝典 编辑:程序博客网 时间:2024/06/17 08:23



public class IsLoginFilter : ActionFilterAttribute    {        public override void OnActionExecuting(ActionExecutingContext filterContext)        {            //获得Controller类型            Type t =  filterContext.ActionDescriptor.ControllerDescriptor.ControllerType;            //获得方法名            string actionname = filterContext.RouteData.Values["action"].ToString();            //是否有该特性            bool b = isThatAttribute<UniteAttribute>(actionname, t);            base.OnActionExecuting(filterContext);        }        public bool isThatAttribute<T>(string actionname, Type t)         {            int length = t.GetMethod(actionname).GetCustomAttributes(typeof(T), true).Length;            return length > 0 ? true : false;        }        public override void OnActionExecuted(ActionExecutedContext filterContext)        {            base.OnActionExecuted(filterContext);        }    }
            string controllername = filterContext.RouteData.Values["controller"].ToString();
            string actionname = filterContext.RouteData.Values["action"].ToString();

0 0
原创粉丝点击