How to disable a global filter in asp net mvc selectively


ASP.NET is a popular programming used for building web applications. One common requirement in ASP.NET MVC is to disable a global filter . In this article, we will explore different approaches to achieve this.

Approach 1: Using the [] Attribute

The first approach to selectively disable a global filter in ASP.NET MVC is by using the [AllowAnonymous] attribute. This attribute can be applied to individual controller actions or entire controllers to bypass any global filters that may be applied.

public ActionResult PublicAction()
    // Code for public action

In the above example, the [AllowAnonymous] attribute is applied to the PublicAction method. This will bypass any global filters that may be applied to the controller this action.

Approach 2: Global Filter

approach to selectively disable a global filter is by modifying the global filter collection in the Global.asax.cs file. The global filter collection is a collection of filters that are applied to all controller actions by .

protected void Application_Start()
    // Other code

    // Remove specific filter from global filter collection
    GlobalFilters.Filters.Remove(GlobalFilters.Filters.FirstOrDefault(f => f.Instance is MyFilter));

In the above example, we remove a specific filter (MyFilter) from the global filter collection. This will prevent the filter from being applied to all controller actions.

Approach 3: Using Conditional Filters

Conditional filters provide a way to selectively apply filters based on certain conditions. This can be useful when you want to disable a global filter for specific controller actions or under certain circumstances.

public class MyFilter : ActionFilterAttribute
    public override void OnActionExecuting(ActionExecutingContext filterContext)
        if (ShouldApplyFilter(filterContext))
            // Code for filter logic

    private bool ShouldApplyFilter(ActionExecutingContext filterContext)
        // Condition to determine whether filter  be applied
        return filterContext.HttpContext.Request.Url.AbsolutePath.Contains("admin");

In the above example, the MyFilter class inherits from ActionFilterAttribute and overrides the OnActionExecuting method. Inside this method, we whether the filter should be applied based on a specific condition (e.g., if the URL contains “admin”).


Disabling a global filter selectively in ASP.NET MVC can be achieved using various approaches. The [AllowAnonymous] attribute, modifying the global filter collection, and using conditional filters are some of the methods discussed in this article. Choose the approach that best suits your requirements and implement it in your ASP.NET MVC application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents