您的位置:首页 > 编程语言 > ASP

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

2014-04-11 10:31 176 查看
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: