Configuring content negotiation
In this recipe, you will learn how to manage content negotiation in ASP.NET Core. ASP.NET Core can return data in any format (such as JSON, XML, PLIST, and SOAP), thanks to ContentFormatters
. More information can be found at https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-formatters.
Any client can make a request with a header to tell the server in what format it wants the response. The server application can read a header value, and use it when creating a response to a request. Content negotiation is the name of whole process.
Getting ready
By default, ASP.NET Core will return JSON from action methods.
Let's get the code from the previous recipe, and let's see the result from GetAllProducts
:
We get JSON values, because by default, ASP.NET Core doesn't take into account the Accept
header the browser sent.
How to do it...
- First, let's add the following dependency in the project:
"Microsoft.AspNetCore.MVC.Formatters.Xml": "2.0.0"
- Next, let's add the...