liveBook · Manning

Chapter 22: Creating custom MVC and Razor Page filters

What you will learn in chapter 22 of ASP.NET Core in Action, 3e

This chapter introduction previews creating custom MVC and Razor Pages filters to remove duplicate request logic, short-circuit requests, and use dependency injection in attribute-based filters.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews creating custom MVC and Razor Pages filters to remove duplicate request logic, short-circuit requests, and use dependency injection in attribute-based filters.

Key terms: custom filters, MVC filters, Razor Pages filters, action methods, filter types, MVC pipeline, built-in options, short-circuiting, filter pipeline, middleware

22.1 Creating custom filters for your application

This section introduces refactoring a recipe web API controller by moving duplicated cross-cutting concerns into custom MVC filters.

Key terms: custom filters, cross-cutting code, action methods, recipe controller, Recipe entity, RecipeDetailViewModel, exceptions, duplication, sync filter interfaces, async counterparts

22.1.1 Authorization filters: Protecting your APIs

This section introduces MVC authorization filters and shows how the built-in [Authorize] attribute protects actions, controllers, Razor Pages, or an entire application.

Key terms: authentication, authorization, authorization filters, MVC filter pipeline, short-circuiting, AuthorizeFilter, [Authorize], authenticated user, 401 Unauthorized, controller

22.1.2 Resource filters: Short-circuiting your action methods

This section shows how resource filters run around MVC processing to enforce early cross-cutting rules and short-circuit requests before an action executes.

Key terms: resource filters, model binding, MVC filter pipeline, FeatureEnabledAttribute, IsEnabled, feature toggle, cross-cutting logic, resource filter attribute, context.Result, IActionResult

22.1.3 Action filters: Customizing model binding and action results

This section shows how MVC action filters use post-binding request data and pre/post-action hooks to centralize validation, entity checks, response changes, and controller-wide logic.

Key terms: action filters, model binding, IActionResult, action method, ActionFilterAttribute, ValidateModelAttribute, ModelState, context.Result, BadRequestResult, [ApiController]

22.1.4 Exception filters: Custom exception handling for your action methods

This section explains when MVC exception filters are preferable to global exception-handling middleware, what exceptions they can catch, and how they enable API-specific error responses.

Key terms: exception filters, exception-handling middleware, MVC framework, cross-cutting concerns, action parameters, exception filter, web API controller, Razor Pages, exception handler middleware, JSON

22.1.5 Result filters: Customizing action results before they execute

This section explains how MVC result filters customize IActionResult execution, including output formatting and last-minute response changes.

Key terms: result filters, IActionResult, action filters, context.Result, short-circuiting, ProducesAttribute, FormatFilterAttribute, output formatters, Accept header, format

22.1.6 Page filters: Customizing model binding for Razor Pages

This section explains how Razor Page page filters parallel action filters, how they access bound values, and when their use is worthwhile.

Key terms: page filters, action filters, IPageFilter, PageResponseCacheFilter, ResponseCacheFilter, OnPageHandlerExecuting, model binding, model validation, page handler, HandlerArguments

22.2 Understanding pipeline short-circuiting

This section explains how MVC and Razor Pages filter short-circuiting changes pipeline execution and when result filters can still run.

Key terms: short-circuiting, context.Result, IActionResult, action filters, page filters, result filters, resource filters, Executing, Executed, context.Cancelled

22.3 Using dependency injection with filter attributes

This section shows how to use dependency injection in MVC and Razor Pages filters by separating filter behavior from attributes and choosing TypeFilterAttribute or ServiceFilterAttribute.

Key terms: attributes, dependency injection, singleton, transient, scoped, EnsureRecipeExistsFilter, EnsureRecipeExistsAttribute, IFilterFactory, CreateInstance(), RecipeService

22.4 Summary

This summary recaps the MVC and Razor Pages filter types, their execution roles and short-circuiting behavior, and how to use dependency injection in custom filter attributes.

Where this is used later

Concepts explained in this chapter

← Chapter 21: The MVC and Razor Pages filter pipeline Chapter 23: Authentication: Adding users to your application with Identity →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.