liveBook · Manning

ConfigureApiBehaviorOptions

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

ConfigureApiBehaviorOptions is an ASP.NET Core MVC builder method that configures the behavior options applied to API controllers.

ConfigureApiBehaviorOptions customizes the conventions applied to ASP.NET Core API controllers. Call it on the IMvcBuilder returned by AddControllers in Program.cs when the defaults do not fit your API, such as an existing specification. The default conventions usually reduce boilerplate by binding request bodies automatically, returning 400 responses for invalid models, and producing ProblemDetails responses. section 20.4 section 20.7

Disable automatic validation responses

builder.Services.AddControllers() .ConfigureApiBehaviorOptions(options => { options.SuppressModelStateInvalidFilter = true; }); This leaves invalid-model handling to controller code instead of automatically returning a 400 Bad Request response. Keep the defaults unless custom behavior is genuinely needed. section 20.4

Listing 20.12 Customizing [ApiAttribute] behaviors
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
 
builder.Services.AddControllers();
    .ConfigureApiBehaviorOptions(options =>               #1
    {
        options.SuppressModelStateInvalidFilter = true;   #2
    });
 
// ...

Inspect the complete service-registration example showing where the configuration lambda belongs and which option disables the automatic invalid-model filter. section 20.4

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

How deep it goes
Explains service registration and one validation override, but does not enumerate every configurable API-controller behavior.
Examples
Uses AddControllers configuration to set SuppressModelStateInvalidFilter to true.
What it assumes
Assumes an ASP.NET Core MVC service setup with AddControllers and introduces configuration through the returned IMvcBuilder.
What you can do after
Configure API-controller conventions through an AddControllers lambda and take manual control of invalid-model responses.
How it connects
Connects ApiController conventions with model validation, automatic 400 responses, ProblemDetails, and minimal APIs.

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.