liveBook · Manning

Input formatter

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

An input formatter is an ASP.NET Core component that reads request content in a supported media format and converts it into application data.

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

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

How deep it goes
Defines format conversion and configuration, but defers custom formatter implementation to external documentation.
Examples
Uses JSON request bodies, XML formatter registration, XML requests, and CSV as a possible custom format.
What it assumes
Assumes familiarity with HTTP request bodies and model binding while showing that [FromBody] selects body binding.
What you can do after
Configure XML support and bind supported JSON or XML request bodies to action parameters.
How it connects
Connects input formatters to model binding, [FromBody], API controllers, output formatters, and content negotiation.

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.