ASP.NET Core Web API - Configure Formats

ASP.NET Core Web API Configure Formats. In this article, we will learn how to configure formats in ASP.NET Core Web API.

In this article, we will learn how to configure formats in ASP.NET Core Web API like,
  • Support specific format for specific action as example XML
  • Support specific format as a default format as example XML
JSON is the default format in ASP.NET Core but assume you need, for some reasons, to support XML format globally. Doing that is easy.
 
Step 1
 
Open Visual Studio.
 
Visual Studio 
 
Step 2
 
Create a new project.
 
Visual Studio - Create new project 
 
Step 3
 
Select Web >> ASP.NET CORE Web Application
 
ASP.NET CORE Web Application 
 
Step 4
 
Select Web API.
 
Asp.Net Core Web API 
 
Step 5
 
Add a new folder and name it Models. Then, add a new class with the following code and name it Employee.
  1. public class Employee  
  2. {  
  3.     public int Id;  
  4.     public string FirstName;  
  5.     public string LastName;  
  6. }  
Step 6
 
Open ValuesController.cs
 
Asp.Net Core Web API 
 
Step 6
 
Modify "Get" method to return the list of employees.
  1. [HttpGet]  
  2. public List<Employee> Get()  
  3. {  
  4.     return new List<Employee> { new Employee { Id = 1, FirstName = "Omar", LastName = "Maher" },  
  5.                                 new Employee { Id = 2, FirstName = "Ahmed", LastName = "Ali" }};  
  6. }  
Run the project by pressing F5. You will get the following result.
 
Asp.Net Core Web API 
 
Step 7
 
To support XML format, you should add Microsoft.AspNetCore.Mvc.Formatters.Xml package.
 
Step 8
 
Right click on the project and select "Manage NuGet Packages".
 
Manage NuGet Packages 
 
Step 9 
 
Type Microsoft.AspNetCore.Mvc.Formatters.Xml in the box and install it.
 
Microsoft.AspNetCore.Mvc.Formatters.Xml 
 
Step 10
 
Open Startup.cs file and edit ConfigureServices.
  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3.     services.AddMvc().AddXmlSerializerFormatters();  
  4. }  
AddXmlSerializerFormatters() method will add XML format to our project.
 
Step 11
 
Coem back to the ValuesController.cs file and edit Get method to add Produces filter.
 
Produces filter takes the type of format that you want to use. In our example, we are trying to use XML format. So, we will pass “application/xml".
  1. [HttpGet]  
  2. [Produces("application/xml")]  
  3. public List<Employee> Get()  
  4. {  
  5.     return new List<Employee> { new Employee { Id = 1, FirstName = "Omar", LastName = "Maher" },  
  6.                                 new Employee { Id = 2, FirstName = "Ahmed", LastName = "Ali" }};  
  7. }  
Step 12
 
Run the project. You will get the following result.
 
Asp.Net Core Web API 
 
Step 13
 
To ensure if other actions still return JSON,  edit Get(int id) as shown below.
  1. // GET api/values/1  
  2. [HttpGet("{id}")]  
  3. public Employee Get(int id)  
  4. {  
  5.     var employees = new List<Employee> { new Employee { Id = 1, FirstName = "Omar", LastName = "Maher" },  
  6.                                 new Employee { Id = 2, FirstName = "Ahmed", LastName = "Ali" }};  
  7.   
  8.     return employees.Where(e=>e.Id == id).Select(e=>e).FirstOrDefault();  
  9. }  
Step 14
 
Run the project. You will see the JSON result.
 
Asp.Net Core Web API JSON 
 
Now, what if you want to support XML for all the applications?
 
Step 1
 
Go back to Startup.cs file and add edit ConfigureServices.
 
Add the filtter here to be applied in the whole application.
  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3.     services.AddMvc(options =>  
  4.     {  
  5.         options.Filters.Add(new ProducesAttribute("application/xml"));  
  6.     })  
  7.     .AddXmlSerializerFormatters();  
  8. }  
Step 2
 
Run the project.
 
Asp.Net Core Web API 
 
Summary
 
Changing the format in ASP.NET Core is very easy and flexible.
 
I hope this article will help you someday in real world apps.