liveBook · Manning

Global filter

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

A filter registered globally so that it applies across the application's eligible controllers, pages, actions, or handlers.

A global filter applies across an application's eligible endpoints without repeating attributes on controllers, Razor Pages, actions, or handlers. Action filters affect action endpoints; page filters affect Razor Page endpoints. section 30.6

Configure global filters through MVC options in Program.cs:

  • Use opts.Filters.Add<T>() for a filter type. section 30.6
  • Use AddService<T>() when the filter is also registered as a service. section 30.6
  • Use the nongeneric Add method to register a specific filter object. section 30.6
  • Expect global filters before controller- or page-model filters, followed by action- or handler-level filters. section 30.7
  • Implement IOrderedFilter when sequencing matters; lower Order values run earlier, and negative values can precede default global filters. section 30.7

Apply HTTPS enforcement globally

Register `HttpsOnlyAttribute` with `builder.Services.Configure<MvcOptions>(opts => opts.Filters.Add<HttpsOnlyAttribute>())`, remove `[HttpsOnly]` from `HomeController`, restart the application, and request `http://localhost:5000`. The HTTPS-only policy still applies, and an authorization filter can short-circuit the pipeline. section 30.6

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

How deep it goes
Explains registration, endpoint-specific applicability, default scope order, and IOrderedFilter customization; it stops short of filter implementation details.
Examples
Uses HTTPS enforcement, MessageAttribute instances across global, controller, and action scopes, and requests to localhost URLs.
What it assumes
Assumes familiarity with ASP.NET Core controllers, Razor Pages, attributes, filters, Program.cs, and dependency-injected services.
What you can do after
Operate/configure global filters in Program.cs, remove redundant controller attributes, and verify behavior through concrete HTTP requests.
How it connects
Connects MVC options, action filters, page filters, result filters, authorization filters, Razor Pages, and the filter pipeline.

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.