What is the Filter Scope for and why the weird names?
Asked Answered
H

3

8

According to this documentation for ASP.NET Filters filters run in the following order:

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

The within each filter type there is a filter Order which specifies the run order.

Makes sense so far... but then it gets bizarre.

There is a further method of ordering within each filter type and order which is represented as an enumeration of the following values:

public enum FilterScope
{
    First = 0,
    Global = 10,
    Controller = 20,
    Action = 30,
    Last = 100,
}

What bearing does Global, Controller and Action have within the run order for an action filter?

For example:

If I have two Action Filters, both with a run order of 1 and FilterScope of Controller and Action respectively.

Other than ordering one in front of the other, what bearing does Controller and Action have on anything?

Further Bizarreness

According to this the FilterScope provides third level ordering for filters. How is Controller, Global, or Action an order for a filter that is in no way restricted for use on just a Controller, Action and not necessarily applied globally? It isn't descriptive of an order.

Also, if it does provide third level filtering, why is it restricted to just the 5 options?

Heidi answered 17/8, 2015 at 11:56 Comment(7)
I think FilterScope defines where you use the filter: globally, on the whole controller or on some action. So the first one that will execute will be a global filter, then controller and then actionRosenthal
Does that help you: msdn.microsoft.com/en-us/library/… ?Rational
@VsevolodGoloviznin I thought the same, but that doesn't explain why it's specified for each filter type and order, it would surely be overarching these. So it would be: Filter Scope, Filter Type, Filter Order. Not the other way around?Heidi
@Serv Sadly not, that page just contains the same information as the documentation that I posted in the question :(Heidi
It seems as though the FilterScope is just for setting the order of the filter when used amongst multiple filters, but to me that's what the Order is for. Why would I want to set my filter to be Order = 0 (first) and FilterScope = Last. It doesn't make any sense. It doesn't restrict the usage of a filter, it just sets the orderingHeidi
Unless you can find some class I can't, so far as I can see, you don't get to control the Scope value - it's implicitly assigned based on usage. So I think this is basically tie-breaking rules since, at the same level, you're not allowed to assign the same Order to multiple filters, but when considering global, controller and actions you may end up with 3 filters all (validly) with the same Order.Monseigneur
It's funny you should say that, because I just commented on the answer by @VMAtm with words to that effect, I can't find any examples where the FilterScope value is set.Heidi
M
3

Filter objects, the objects that actually have a Scope property, are constructed based on usage - when you add a filter to the global application filters, a Filter object is constructed with a Scope of Global. Similarly, when filter attributes are collected from the controller and the action, Filter objects are constructed using scopes of Controller and Action, respectively.

I'm not entirely sure how a Filter with a Scope of First or Last actually gets created.

These rules are specified to say how tie-breaking will be applied should you have a filter declared at, say, the global level and at the action level using the same Order value - which is more of a concern than filters declared at the same level where you're expected to manually ensure that each filter uses a unique Order (if you care about ordering).

Monseigneur answered 17/8, 2015 at 13:36 Comment(1)
I think the default is First as it is a first in enum, which leads it's equality to 0Truffle
T
2

Well, I can't really understand what exactly do you find as a bizarreness here.

Authorization, Action, Response and Exception filters are 4 interfaces you can implement to run the filter logic, IAuthorizationFilter, IActionFilter, IResultFilter and IExceptionFilter interfaces respectively.

After that, the business rules comes out to the lights. For example, you have to check the access rights for some user action. You have not only implement the authorization filter, but create a logic for checking rules like:

  • If user didn't finish registration, you have to remind him about this. This rule should be run at First, no matter what user does on your site.
  • If user hasn't been approved, he can't view site contents, and should gain a validation message. So it is a Global scoped rule, and it should be run before any other checks for user rights.
  • If user has no access for some department, he can't view some content on site regarding that department, but not all - so we let the Controller choose, what should be displayed to user.
  • If user isn't a manager, he can't edit or delete some content. So, this is a concrete Action which is being filtered.
  • We can fire up some logger after task is being processed, so we have to wait until the work is done, and run filter at Last.

I see here a very simple model for the filter ordering, and I can provide a sample for each pair or filter type/filter scope.

Update:

some sample code for a Filter's ordering:

public class ControllerInstanceFilterProvider : IFilterProvider {
    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) {
        if (controllerContext.Controller != null) {
            // Use FilterScope.First and Order of Int32.MinValue to ensure controller instance methods always run first
            yield return new Filter(controllerContext.Controller, FilterScope.First, Int32.MinValue);
        }
    }
}
Truffle answered 17/8, 2015 at 13:14 Comment(4)
Thanks for the answer. According to the docs, the filter scope is third level ordering, not for setting where it is appliedHeidi
I can't find a single example of the FilterScope being set either, can you?Heidi
@Coulton Yes, I've stated that this is only about ordering, and should be used for complicated filter hierarchy.Truffle
@Coulton Found only one, not on MSDN. I think this is an over-engeneering, but still can be used in rare casesTruffle
R
0

As per my understanding,

There is filter order which means, order in which filter runs. So filter runs in the following order:

  1. Authorisation Filter
  2. Action Filter
  3. Response Filter
  4. Exception Filter

Within each filter type, the Order Value specify the run order. And within each filter type and order, the SCOPE enumeration value specifies the order for filter.

For eg: An OnActionExecuting() filter that has the Order property set to zero and filter scope set to first runs before an action filter that has the order property set to zero and filter scope set to Action.

Order and scope are used for prioritising internally between each filter type.

Reconstructive answered 29/3, 2020 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.