Area-global filters in MVC 3
Asked Answered
E

2

6

Since it doesn't appear to be supported natively, what's the recommended way of implementing global filters on a per-area basis?

Emilie answered 18/4, 2011 at 8:28 Comment(2)
Putting the filter on every controller of that area? :) But seriously, do you mean ActionFilters?Myogenic
I do, but as of MVC 3 ActionFilters are a specific implementation of the broader IFilterEmilie
F
2

You could use a custom IFilterProvider.

Forejudge answered 18/4, 2011 at 8:31 Comment(1)
That should do it! Good thinkingEmilie
W
9

The reason it's not supported natively is because we want to discourage developers from doing it. Areas are not associated with controllers; they're associated with routes. We wanted to discourage developers from trying to apply authorization or other security-sensitive filters to areas since they can be bypassed.

See How can we set authorization for a whole area in ASP.NET MVC? for more information.

Wimsatt answered 20/4, 2011 at 5:39 Comment(4)
For an MVC 4 update, see blogs.msdn.com/b/rickandy/archive/2012/03/23/…Boorman
"Areas are not associated with controllers" -- funny that I can see a controllers folder in each area. Controllers are always associated with an area: either the "root" area, or a custom area. So I disagree. A common example of areas is to create an "Admin" area - guess what filter you'd want to apply globally to just that area.Gonsalve
@Gonsalve Your assumption is incorrect. See also my answer to #2319657, which demonstrates that controllers don't belong to areas.Wimsatt
Thanks for explaining that: I now understand what you mean better, and withdraw my previous comment. It sounds like the controller factory is being too helpful in matching stuff up.Gonsalve
F
2

You could use a custom IFilterProvider.

Forejudge answered 18/4, 2011 at 8:31 Comment(1)
That should do it! Good thinkingEmilie

© 2022 - 2024 — McMap. All rights reserved.