MVC 3 GlobalFilters Exclude
Asked Answered
C

4

9

I have a filter that would like to apply to all controllers except for one. So I am trying to write something that looks like this:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
 {
     filters.Add(new MySweetAttribute()).Exclude(OneController);
 }

Trying to read through Brad's post on the subject is gibberish to me

http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html

I am assuming it is possible since the library below seems to do it, but I would like to avoid adding a dependency if possible.

http://www.codeproject.com/KB/aspnet/FluentFltrsASPNETMVC3.aspx

Hoping someone has done this already and it is easy to do...

Thanks for any help.

Update

Phil Haack has just posted how to approach this scenario.

http://haacked.com/archive/2011/04/25/conditional-filters.aspx

Chasseur answered 21/2, 2011 at 20:13 Comment(0)
T
2

I think you will need to implement a filter provider to do this, then when you implement GetFilters do not apply the filter for the action you wish to exclude. Here is an example:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=578

Telfer answered 21/2, 2011 at 20:26 Comment(0)
T
32

I've been searching the web for the same question without luck so I just tried this myself and it works:

public class MySweetAttribute: ActionFilterAttribute
{
    public bool Disable { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (Disable) return;

        ...
    }
}

Then, when you want to disable the global filter, just add the attribute to the action with the disable propierty set to true:

[MySweetAttribute(Disable=true)]
public ActionResult Index()
{            
    return View();
}

Hope this help

Tweeze answered 8/4, 2011 at 20:43 Comment(4)
Amazing and fast solution! :DReynoso
U R Z 1. Though, I have found many other answers, but this still the simplest and most efficient answer all over the globe.Chasten
This is my preferred approach as wellOtt
One little "gotcha" I've noticed here - if you specify an Order in the global declaration of the filter, you must also specify the same order on on the attribute. Eg [DataAccessAttribute(Disable=true,Order=2)] and filters.Add(new DataAccessAttribute(), 2);Ephrayim
F
2

You can't exclude from Global filters. If you want controllers to be excluded use standard filters.

Frame answered 21/2, 2011 at 20:26 Comment(2)
Ok, so is there an easy to way to apply a filter to all but one controller? Readying through the new FilterProvider...Chasseur
You could have all controllers that you want to have the filter applied to derive from a common base controller.Frame
T
2

I think you will need to implement a filter provider to do this, then when you implement GetFilters do not apply the filter for the action you wish to exclude. Here is an example:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=578

Telfer answered 21/2, 2011 at 20:26 Comment(0)
B
2

Implementing IFilterProvider is not so complicated. See full sample which uses the provider facility to exclude a filter by type: http://blogs.microsoft.co.il/blogs/oric/archive/2011/10/28/exclude-a-filter.aspx

Brewington answered 29/10, 2011 at 1:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.