public class GlobalActionFilter : ActionFilterAttribute
{
private string _requestId;
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext); //方法1. var gaf=actionContext.ActionDescriptor.GetCustomAttributes<GlobalAuthorizationFilter>().FirstOrDefault();
string methodName1 = actionContext.ActionDescriptor.ActionName;
//var ad=actionContext.ActionDescriptor.MethodInfo; //MethodInfo调试可以看到,但是代码点不出来
//方法2:反射
var methodActionDescriptorProperty = actionContext.Request.Properties["MS_HttpActionDescriptor"] as ReflectedHttpActionDescriptor;
if (methodActionDescriptorProperty != null)
{
var methodInfo = methodActionDescriptorProperty.MethodInfo; //获取方法详情
string methodName = methodInfo.Name; //方法名
var methodCustomAttributes = methodInfo.GetCustomAttributes(true).ToList(); //获得所有自定义的attributes标记 var authAttribute = methodCustomAttributes.FirstOrDefault(ee => ee is GlobalAuthorizationFilter); //查找是否有特定的attribute标记
if (authAttribute != null)
{
var gf = authAttribute as GlobalAuthorizationFilter; //转换为特定的attribute
bool isNeedLogin = gf.IsNeedLogin; //拿到特定标记的字段 }
} }
From:http://www.cnblogs.com/xuejianxiyang/p/6208406.html