如何在ActionFilter中获取参数的所有属性?

时间:2015-10-02 14:09:21

标签: asp.net-mvc data-annotations

我有一个控制器:

public class CatController
{

public something DoStuff([FromBody]string bibble)
{

}

}

我想创建一个ActionFilter并循环遍历每个参数,并查看该参数具有哪些属性,例如[FromBody]。我怎么能这样做?

public class InspectParametersActionFilter : ActionFilterAttribute, IActionFilter
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        actionContext.ActionDescriptor.?
        actionContext.ActionArguments.?
    }
}

1 个答案:

答案 0 :(得分:2)

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var allCustomAttributes = filterContext.ActionDescriptor.GetParameters()
        .Select(s => s.GetCustomAttributes(false));
}