liveBook · Manning

Resource filter

As explained in Pro ASP.NET Core 7, Tenth Edition

A resource filter is an ASP.NET Core MVC filter that runs before model binding and after endpoint execution but before the action result is executed.

A resource filter intercepts ASP.NET Core MVC requests around endpoint execution. It runs before model binding, then resumes after the endpoint has handled the request but before the action result executes. This makes it useful for changing requests, changing responses, or stopping the pipeline early. section 30.3 section 30.4.2

Use a resource filter when you need to:

  • Short-circuit processing by assigning an IActionResult to ResourceExecutingContext.Result. section 30.4.2
  • Implement request-level features such as caching, before model binding and endpoint work occur. section 30.3 section 30.4.2
  • Inspect the eventual action result, cancellation state, or exception through ResourceExecutedContext. section 30.4.2

A simple path-based cache

A SimpleCacheAttribute can implement IResourceFilter, use the request PathString as a dictionary key, and assign a previously stored IActionResult to context.Result when a cached entry exists. Otherwise, the endpoint runs and OnResourceExecuted can store its result. Applied alongside [RequireHttps] on a Razor Page model, the filter demonstrates reusable caching at the endpoint boundary.

Table 30.2 The filter types (view table figure)

Name

Description

Authorization filters

This type of filter is used to apply the application’s authorization policy.

Resource filters

This type of filter is used to intercept requests, typically to implement features such as caching.

Action filters

This type of filter is used to modify the request before it is received by an action method or to modify the action result after it has been produced. This type of filter can be applied only to controllers and actions.

Page filters

This type of filter is used to modify the request before it is received by a Razor Page handler method or to modify the action result after it has been produced. This type of filter can be applied only to Razor Pages.

Result filters

This type of filter is used to alter the action result before it is executed or to modify the result after execution.

Exception filters

This type of filter is used to handle exceptions that occur during the execution of the action method or page handler.

Compare resource filters with authorization, action, page, result, and exception filters to choose the pipeline stage that matches the feature. section 30.3

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Defines synchronous and asynchronous interfaces, lifecycle contexts, short-circuiting, caching, and attribute application without detailing complete production caching design.
Examples
Demonstrates a path-keyed SimpleCacheAttribute applied to a Razor Page model alongside RequireHttps.
What it assumes
Assumes ASP.NET Core MVC request processing and endpoint concepts while identifying model binding and filter categories through their pipeline positions.
What you can do after
Build it from scratch as a synchronous or asynchronous filter that can inspect, cache, alter, or short-circuit endpoint processing.
How it connects
Connects resource filters to model binding, action filters, page filters, result filters, middleware, caching, and dependency injection.

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 Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.