liveBook · Manning

Action filter attribute

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

ActionFilterAttribute is an ASP.NET Core abstract base class that provides synchronous and asynchronous MVC action- and result-filter behavior.

ActionFilterAttribute is an ASP.NET Core abstract base class for synchronous and asynchronous MVC action- and result-filter behavior. Derive from it when a filter needs only selected lifecycle methods. section 22.1.3

Action filters run after model binding, so ModelState contains validation results. Assigning context.Result short-circuits action execution and later action filters while the remaining pipeline continues. section 22.1.3

Validate invalid model state

Override OnActionExecuting, test context.ModelState.IsValid, and assign BadRequestObjectResult(context.ModelState) when validation fails. This moves repeated controller checks into the filter pipeline. section 22.1.3

Return 404 for a missing entity

An EnsureRecipeExistsAttribute retrieves RecipeService from request services, reads the action's id argument, and assigns NotFoundResult when the entity is absent.

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

How deep it goes
Explains inheritance, lifecycle overrides, model-binding timing, and short-circuit behavior through working filter examples.
Examples
Uses ModelState validation and Recipe existence checks to demonstrate Bad Request and Not Found short-circuiting.
What it assumes
Assumes model binding, action arguments, ModelState, and the MVC filter pipeline are recognizable concepts.
What you can do after
Apply it via ASP.NET Core MVC to remove repeated validation and existence checks from action methods.
How it connects
Connects MVC action filters with model binding, ModelState, result filters, dependency injection, and ApiController behavior.

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.