webapi filter过滤器中获得请求的方法详情(方法名,Attributes)

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; //拿到特定标记的字段 }
} }

webapi filter过滤器中获得请求的方法详情(方法名,Attributes)

From:http://www.cnblogs.com/xuejianxiyang/p/6208406.html

上一篇:执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”


下一篇:【转】DevOps的前世今生