如何获取ActionFilter中已注册GlobalFilter的列表

时间:2019-04-07 05:59:51

标签: asp.net-mvc

我创建了一个自定义ActionFilterAttribute,并希望在通过某些属性修饰动作时执行某些操作。我可以获得动作方法的属性和控制器的属性。但找不到在FilterConfig中注册的属性。

如何找到它们?

我的代码是:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new SomeFilterOrAttribute());
} 

public class BeforeActionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var actionAttributes = filterContext.ActionDescriptor.GetCustomAttributes(true);
        var controllerAttributes = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(true);

        // How can get list of registered global filters or attributes? 
    }
}

1 个答案:

答案 0 :(得分:1)

GlobalFilters类允许检索和修改全局过滤器集合

//requires "using System.Linq"
var globalFilters = GlobalFilters.Filters.ToList();