判斷MVC的Controller/Action是否包含某種特性兩種方式
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//獲得Controller的特性
var controllerSkip = (filterContext.ActionDescriptor as ControllerActionDescriptor).MethodInfo.ReflectedType.GetCustomAttributes(typeof(SkipLoginAttribute), true).Length;
//獲取action的特性
var actionSkip = (filterContext.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(typeof(SkipLoginAttribute), true).Length;
//獲得Controller的特性
var test1 = (filterContext.ActionDescriptor as ControllerActionDescriptor).MethodInfo.ReflectedType.CustomAttributes.Where(c=>c.AttributeType==typeof(SkipLoginAttribute)).Count();
//獲取action的特性
var test2 = (filterContext.ActionDescriptor as ControllerActionDescriptor).MethodInfo.CustomAttributes.Where(c => c.AttributeType == typeof(SkipLoginAttribute)).Count();
//獲取controller名稱
var controllerName = (string)filterContext.RouteData.Values["controller"];
//獲取action名稱
var actionName = (string)filterContext.RouteData.Values["action"];
}
判斷MVC的Controller/Action是否包含某種特性兩種方式