What is the scope of the filter and why the weird names?

According to this documentation for ASP.NET filters, filters work in the following order:

  • Authorization Filters
  • Action filters
  • Response filters
  • Exception filters

Inside each type of filter there is an Order filter, which determines the execution order.

It makes sense so far ... but then it gets weird.

There is another way of ordering in each type and order of the filter, which is presented as an enumeration of the following values:

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

What does a global controller and an action have in order to execute an action filter?

For instance:

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

Except for ordering one opposite the other, what does Controller and Action to do with something?

Further Bizarreness

Accordingly, FilterScope provides third-level ordering for filters. How is Controller , Global or Action order for a filter that is in no way limited to be used only on Controller , Action and is not necessarily applied globally? He does not describe the order.

In addition, if it provides filtering at the third level, why is it limited to only 5 parameters?

+6
source share
2 answers

Filter objects that actually have Scope are created on the basis of use - when you add a filter to global application filters, the Filter object is built with Scope from Global . Similarly, when filter attributes are collected from the controller and action, Filter objects are created using the Controller and Action areas, respectively.

I'm not quite sure how Filter is really created with Scope from First or Last .

These rules are indicated to tell how a binding break will be applied if you have a filter declared, say, at the global level and at the action level, using the same Order value, which is more worrying than filters declared at the same level, where you expect to manually verify that each filter uses a unique Order (if you want to order).

+3
source

Well, I can’t understand what exactly you find here as bizarre.

Authorization, action, response, and exception filters are 4 interfaces that you can implement to trigger filter logic, IAuthorizationFilter , IActionFilter , IResultFilter and IExceptionFilter respectively.

After that, business rules come to light. For example, you should check permissions for some user actions. You not only implement an authorization filter, but also create logic to check rules such as:

  • If the user has not completed registration, you must remind him of this. This rule must be followed in First , no matter what the user does on your site.
  • If the user has not been approved, he cannot view the contents of the site and must receive confirmation. Therefore, this is a Global rule, and it must be run before any other user rights checks.
  • If the user does not have access to any department, he cannot view any content on the site regarding this department, but not all - therefore, we allow the Controller to choose what should be displayed to the user.
  • If the user is not a manager, he cannot edit or delete any content. So this is a specific Action that is being filtered.
  • We can run several logs after processing the task, so we need to wait until the work is completed, and run the filter in Last .

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

Update:

some sample code to arrange the filter :

 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); } } } 
+2
source

All Articles