How to disable a global filter in ASP.Net MVC selectively

You could write a marker attribute:

public class SkipMyGlobalActionFilterAttribute : Attribute
{
}

and then in your global action filter test for the presence of this marker on the action:

public class MyGlobalActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(SkipMyGlobalActionFilterAttribute), false).Any())
        {
            return;
        }

        // here do whatever you were intending to do
    }
}

and then if you want to exclude some action from the global filter simply decorate it with the marker attribute:

[SkipMyGlobalActionFilter]
public ActionResult Index()
{
    return View();
}

Leave a Comment