Introducing OutputFormatters
OutputFormatters are classes that transform your existing data into different formats to send it through HTTP to clients. The Web API uses a default OutputFormatters
to turn objects into JSON, which is the default format to send structured data. Other built-in formatters include an XML formatter and a plain text formatter.
With so-called content negotiation, clients are able to decide which format they want to retrieve. The client needs to specify the content type of the format in the Accept
header. Content negotiation is implemented in ObjectResult
.
By default, the Web API always returns JSON, even if you accept text/XML in the header. This is why the built-in XML formatter is not registered by default.
There are two ways to add XmlSerializerOutputFormatter
to ASP.NET Core. The first is shown in the following code snippet:
services.AddControllers() Â Â Â Â .AddXmlSerializerFormatters();
Or, alternatively, you can use the...