MVC 3 GlobalFilters Exclude

I have a filter that I would like to apply to all but one controller. So I'm trying to write something similar to this:

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

Trying to read Brad's post on this is gibberish for me.

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

I assume this is possible because the library seems to be doing this, but I would like to avoid adding a dependency if possible.

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

Hoping someone already done this, and it's 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

+7
asp.net-mvc asp.net-mvc-3
Feb 21 '11 at 20:13
source share
4 answers

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

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

+2
Feb 21 '11 at 20:26
source share

I searched on the Internet the same question with no luck, so I just tried it myself and it works:

 public class MySweetAttribute: ActionFilterAttribute { public bool Disable { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (Disable) return; ... } } 

Then, if you want to disable the global filter, just add the attribute to the action with the propierty parameter disabled to true:

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

Hope for this help

+29
Apr 08 2018-11-11T00:
source share

Implementing IFilterProvider is not that complicated. See The complete sample that the provider tool uses to exclude a filter by type: http://blogs.microsoft.co.il/blogs/oric/archive/2011/10/28/exclude-a-filter.aspx

+2
Oct 29 '11 at 1:12
source share

You cannot exclude from global filters. If you want controllers to be excluded, use standard filters.

+1
Feb 21 '11 at 20:26
source share



All Articles