0

I have registered my actionfilter globally

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

now i need to skip this filter in some methods, the behavior i want is like [AllowAnonymous] how to do it?

danilonet
  • 1,757
  • 16
  • 33

1 Answers1

2

You need to do this in two parts. First, implement your attribute class with which you will decorate the method that you wish to exclude.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ExcludeAttribute : Attribute
{
}

Then, in the ExecuteActionFilterAsync method of your IActionFilter implementation, check if the action being invoked is decorated with this method.

public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
    var excludeAttr = actionContext.ActionDescriptor.GetCustomAttributes<ExcludeAttribute>().SingleOrDefault();

    if (excludeAttr != null) // Exclude attribute found; short-circuit this filter
        return continuation();

    ... // Execute filter otherwise
}
Pranav Negandhi
  • 1,594
  • 1
  • 8
  • 17