.NET Core Web API - XML and Newtonsoft Formatters
.NET Core Web API - XML and Newtonsoft Formatters

XML and NewtonsoftJSON formatters for .NET Core Web APIs

In previous article, we have seen that accept header from http request and return type of action decides how the response would be formatted. In this article, we are going to have look at some more things about how to add two formatters for web APIs.

XML Formatters

As we know, web APIs by default use JSON formatters. So if you use POCO class as return type or an ObjectResult, then it would certainly pick JSON formatter and would return a JSON response.

In some cases, the web API might have clients which want response in XML formats. There might be many reasons why this could happen. One of the reason can be – API was migrated to latest technology but the client is not migrated yet.

By default, .NET core web API supports only supports three accept headers – text/plain, text/json, application/json.

If API is called from postman with Accept http header set to application/xml, it throws HTTP 400 bad request error.

So, if XML formatter is desired, it can be added by calling AddXmlSerializerFormatters method in Startup.ConfigureServices as shown in code snippet below.

Once this change is made in API startup, the client which needs XML response should send application/xml in the Accept header. For example,

curl -X GET "https://localhost:44390/WeatherForecast" -H  "accept: application/xml"

Below code does not mean JSON format is not supported. It simply adds capability to serve XML responses too. Appropriate formatter is chosen based on accept header in the request. It would server xml response if accept header is application/xml and would serve JSON response if accept header is application/json.

If the legacy applications need to use old XML DataContractSerializer, there is another method AddXmlDataContractSerializerFormatters to enable it. It’s usage is not shown above.

Newtonsoft.JSON Formatter

The .NET Core APIs, by default, use JSON formatters from System.Text.Json namespace. Older versions of .NET Core APIs (prior to .NET Core 3.0), APIs used to use formatters from Newtonsoft.Json assembly.

So, there might be several reasons why an application may need newtonsoft.json formatters. Some of those reasons include:

When such app is getting migrated, it may be a useful feature to use Newtonsoft.Json formatters to reduce the amount of code modifications.

A NuGe package – Microsoft.AspNetCore.Mvc.NewtonsoftJson – should be added to the Web API project. This package provides AddNewtonsonftJson which can be called from Startup.ConfigureServices method to setup formatters.

Below is a code example showing its usage.

Enforcing a Formatter

Sometimes, it might be needed to restrict the response formatting to a specific formatting type. In that case, a filter [Produces] can be used. This filter can be placed on controllers or actions or can be applied to global scope as well.

Below code sample shows a controller which is forcing the controller to always return response in JSON format. Similarly, if required, XML formatting can also be enforced.

After specifying this filter, the accept http header is completely ignored and API always produces JSON result.

.NET Core Web API – Force JSON Formatting

I hope you find this information useful. Let me know your thoughts.

Leave a ReplyCancel reply

This Post Has 2 Comments

  1. Nikhil

    Nice Informative article. Good work keep it up….