liveBook · Manning

Filter attribute

As explained in Building Web APIs with ASP.NET Core

A filter attribute is an attribute-based ASP.NET Core component that applies filter behavior to selected controllers or action methods.

A filter attribute is an ASP.NET Core attribute-based component that changes filter-pipeline behavior for selected controllers or action methods. It narrows a configuration change that would otherwise affect the whole application. section 6.2.3

ManualValidationFilterAttribute implements IActionModelConvention. During action-model construction, it finds and removes the ModelStateInvalidFilter, allowing one decorated action to handle ModelState validation manually while automatic validation remains enabled elsewhere. section 6.2.3

Use the attribute when a single action needs different validation behavior:

  • Create ManualValidationFilterAttribute in the Attributes folder. section 6.2.3
  • Inspect action.Filters for ModelStateInvalidFilter or the internal ModelStateInvalidFilterFactory name, then remove the matching filter. section 6.2.3
  • Decorate the chosen action with [ManualValidationFilter], and restore application-wide automatic ModelState validation. section 6.2.3

Selective manual validation

Decorate DomainsController.Get with [ManualValidationFilter]. That action can inspect ModelState and construct its own ValidationProblemDetails response, while other controllers and methods retain automatic validation. section 6.2.3

Listing 6.7 ManualValidationFilterAttribute
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Infrastructure;

namespace MyBGList.Attributes
{
   public class ManualValidationFilterAttribute 
       : Attribute, IActionModelConvention
   {
       public void Apply(ActionModel action)
       {
           for (var i = 0; i < action.Filters.Count; i++)
           {
               if (action.Filters[i] is ModelStateInvalidFilter
                   || action.Filters[i].GetType().Name == 
                       "ModelStateInvalidFilterFactory")
               {
                   action.Filters.RemoveAt(i);
                   break;
               }
           }
       }
   }
}

Inspect the complete convention implementation that removes the model-state invalid filter. section 6.2.3

[HttpGet(Name = "GetDomains")]
[ResponseCache(Location = ResponseCacheLocation.Any, Duration = 60)]
[ManualValidationFilter]                                              #1
public async Task<ActionResult<RestDTO<Domain[]>>> Get(
    [FromQuery] RequestDTO<DomainDTO> input)

See the attribute applied directly to the selected action. section 6.2.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains implementation through IActionModelConvention, filter inspection, attribute application, and configuration cleanup.
Examples
Uses ManualValidationFilterAttribute on DomainsController.Get alongside manual ValidationProblemDetails construction.
What it assumes
Assumes familiarity with ASP.NET Core controllers, action methods, attributes, ModelState, and the filter pipeline.
What you can do after
Build it from scratch to give one action manual validation while preserving automatic validation elsewhere.
How it connects
Connects action filters with model binding, ModelState validation, ApiController, and HTTP request-response behavior.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.