liveBook · Manning

Resource filter

As explained in ASP.NET Core in Action, 3e

A resource filter is an ASP.NET Core MVC filter that runs early in request processing, before model binding, and can also run after the result executes.

A resource filter is an ASP.NET Core MVC filter for logic that must run early in request processing. It runs after authorization filters and before model binding, action execution, and result execution; its executed method runs after the remaining pipeline completes. section 22.1.2 section 22.4

Use a resource filter to:

  • Stop a request before model binding or the action runs by assigning an IActionResult to context.Result. section 22.1.2
  • Control model binding, reject unsupported content types, or apply MVC-specific cross-cutting logic. section 21.1
  • Apply behavior globally, to a controller, to a Razor Page, or to an action. section 21.7
  • Choose IResourceFilter for synchronous work or IAsyncResourceFilter when called services require asynchronous operations; implement only one interface. section 21.4

Feature-gated API

A feature toggle can move from repeated action code into a reusable resource filter. FeatureEnabledAttribute exposes IsEnabled; OnResourceExecuting assigns BadRequestResult when the feature is disabled, so model binding and the action are skipped. Apply it with [FeatureEnabled(IsEnabled = true)] on the controller. section 22.1.2

Listing 22.4 The FeatureEnabledAttribute resource filter
public class FeatureEnabledAttribute : Attribute, IResourceFilter
{
    public bool IsEnabled { get; set; }                 #1
    public void OnResourceExecuting(                    #2
        ResourceExecutingContext context)               #2
    {
        if (!IsEnabled)                                 #3
        {                                               #3
            context.Result = new BadRequestResult();    #3
        }                                               #3
    }
    public void OnResourceExecuted(                     #4
        ResourceExecutedContext context) { }            #4
}
#1 Defines whether the feature is enabled
#2 Executes before model binding, early in the filter pipeline
#3 If the feature isn’t enabled, short-circuits the pipeline by setting the context.Result property
#4 Must be implemented to satisfy IResourceFilter, but not needed in this case

Inspect the complete synchronous filter that checks a feature flag and short-circuits the pipeline. section 22.1.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines synchronous and asynchronous interfaces, execution points, scopes, and short-circuiting, without detailing advanced pipeline internals.
Examples
Uses console-logging filters, content-type rejection, and a feature-enabled API controller.
What it assumes
Assumes basic C# and MVC request concepts while introducing filter stages, contexts, attributes, and application scope.
What you can do after
Build it from scratch with IResourceFilter or IAsyncResourceFilter and short-circuit requests through context.Result.
How it connects
Connects resource filters to authorization filters, model binding, action filters, result filters, middleware, and IActionResult.

Also explained in

Compare how 2 books explain resource filter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.