天天看点

C#MVC中Controler的自定义属性使用

1、首先声明自定义属性

例如:

    public class NeedPassAttribute:Attribute

    {

        /// <summary>

        /// 需要登录(导航到小登录页) 

        /// </summary>

        public bool NeedLogin { get; set; }

    }

2、在Controler的基类继承类中重载OnActionExecuting(ActionExecutingContext filterContext)方法中判断并处理

例如:var attrNeeds = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NeedPassAttribute), false);

继续阅读