A formatter turns an API model into a response representation, such as JSON or XML. In a web API controller, an output formatter fills the role that a Razor view fills in a page application: it produces the content sent to the caller. section 20.2
Returning a model
An action such as `public IEnumerable<string> ListCars() => new[] { "Nissan Micra", "Ford Focus" };` returns data, not a ready-made response body. An `IOutputFormatter` serializes that data. If a client requests XML with an `Accept: text/xml` header but XML support is not configured, the response falls back to JSON by default. section 20.5
The client’s `Accept` header and the available formatters guide content negotiation. By default, ASP.NET Core supports `application/json`, `text/plain`, and `text/html`. With no `Accept` header, a string is formatted as plain text, while other API models typically use JSON; a null model produces `204 No Content`. A browser header containing `*/*` normally bypasses negotiation and uses the default formatter. section 20.5.1 section 20.5.2
services.AddControllers() .AddXmlSerializerFormatters();
Add `.AddXmlSerializerFormatters()` after `AddControllers()` to support XML responses and XML request bodies. An `Accept: text/xml` request can then receive XML. section 20.5.1 section 20.7
For different JSON behavior, `.AddNewtonsoftJson()` replaces the default System.Text.Json formatter. ASP.NET Core 3.0 onward uses System.Text.Json by default; its stricter deserialization can matter when incoming JSON does not meet its expectations. section 20.5.1
Listing 20.14 Customizing MVC to respect the browser’s Accept header in web APIs
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(options => #1 { options.RespectBrowserAcceptHeader = true; #2 options.OutputFormatters.RemoveType<StringOutputFormatter>(); #3 }); #1 AddControllers has an overload that takes a lambda function. #2 False by default; several other properties are also available to be set. #3 Removes the output formatter that formats strings as text/plain
Set `RespectBrowserAcceptHeader` to `true` to honor a browser’s `Accept` header. The same configuration removes `StringOutputFormatter` if strings should no longer be returned as `text/plain`. section 20.5.2

