An input formatter reads request content in a supported media format and converts it into application data during model binding. JSON is configured by default; additional formatters can accept formats such as XML or CSV. section 20.2 section 16.2.3
Input formatters work with request binding in several practical ways:
- A [FromBody] parameter tells the binder to read the request body and use a configured formatter, such as the JSON formatter. section 16.2.3
- Only one parameter can use [FromBody], because the incoming request body can safely be read only once. section 16.2.3
- Adding XML serializer formatters enables XML input; without XML support, an XML request body produces a 415 Unsupported Media Type response. section 20.5.1
- Custom formatters can support other data types, including CSV. section 20.2
Accepting XML in an API controller
Configure the MVC builder with AddXmlSerializerFormatters(). The controller can then receive XML request bodies as well as JSON, provided the request identifies the supported media type. section 20.5.1
services.AddControllers() .AddXmlSerializerFormatters();
The formatter registration shows the small Program.cs change that enables XML request and response handling. section 20.5.1
