Asp.Net MVC Exploring Controllers in depth Part 7. Best Dot Net Training

Asp.Net MVC Exploring Controllers in-depth Part 7. Best Dot Net Training

Filter Overrides in ASP.Net MVC 5.X

Filter Overrides in ASP.NET MVC 5 are very useful when we are implementing a global or controller level filter and we do not want to apply an action filter on some Action methods in the controller. This feature is useful for removing the headache of applying filters for each and every action where we need to exclude only a few actions.

  • Authorization filters
  • Action filters
  • Result filters
  • Exception filters

We have five type filter overrides corresponding to this:

  • OverrideAuthenticationAttribute
  • OverrideAuthorizationAttribute
  • OverrideActionFiltersAttribute
  • OverrideResultAttribute
  • verrideExceptionAttribute

We can mark any action method with an override filter attribute that essentially clears all filters in an       upper scope

  Example: In the following example, the Authorize Filter is applied at the controller level. So, all the action methods of the Home controller can be accessed by the Admin user only. Now I want to exclude the Authorize Filter from the “About” method.

So, in this case, we can mark this “About” method with the “OverrideAuthorization” attribute. Now all the action methods of the home controller can be accessed by the Admin user except the “About” method. We can access the “About” action method without any authorization. (in other words controller level or global level).

Note: The OverrideAuthorizationAttribute does not work properly with MVC version 5.0 due to some internal bug. This bug was resolved in MVC version 5.1 (preview).

Order of Action Filter Execution

Filters run in the following order:

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

Further, the order is controlled by “Order” property:

  1. Unless the Order property is set explicitly, an action filter has an implied order of -1.
  2. If the Order property of multiple action filters is explicitly set, the filter with the lowest value executes before those with greater values, as shown in the following example:

[Filter1(Order = 2)]

[Filter2(Order = 3)]

[Filter3(Order = 1)]

public void Index()

{

View(“Index”);

}

In this example, action filters would execute in the following order: Filter3, Filter1, and then Filter2

  1. If two action filters have the same Order property value, and if one action filter is defined on a type and the other action filter is defined on a method, the action filter defined on the type executes first. The following example shows two an action filter defined on the type and another defined on a

[FilterWithType(Order = 1)]

public class MyController

{

[FilterWithMethod(Order = 1)]

public void Index()

{

View(“Index”);

}

}

* FilterWithType Executes before FilterWithMethod.

 

Visit for MVC Online Training

 

Please follow and like us:
0

Leave a Reply

Your email address will not be published. Required fields are marked *