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

