我想知道如何检查控制器方法是否具有某个属性,例如AllowAnonymous
,在OnActionExecuting
覆盖方法中。
我试过这个:
var methodAttr = Attribute.GetCustomAttribute(context.ActionDescriptor.GetType(), typeof(AuthorizeAttribute));
但我总是得到一个空值。
也试过这个:
MethodBase method = MethodBase.GetCurrentMethod();
AuthorizeAttribute methodAttr = (AuthorizeAttribute)method.GetCustomAttributes(typeof(AuthorizeAttribute), true)[0];
但是当没有 AuthorizeAttribute 我得到一个超出范围的异常。
我该怎么做这个检查?