Combine XML And Json Requests And Responses In ASP.NET Core Web API

Introduction


By default, ASP.NET Core Web API application supports Json data in requests and responses without any additional code. We can add simple code to support XML requests and responses in Web API. We will see all the simple steps in this post.

Create ASP.NET Core 3.1 Web API application in Visual Studio 2019


We can create a simple Web API application in Visual studio with ASP.NET Core 3.1 framework.
 
 
We must modify ConfigureServices method in Startup class with below code. This will add XML data support to both requests and responses in all web methods. Json data format is added by default, in ASP.NET Core Web API application.
 
 
We can create a sample Employee model class to test our application.
 
Employee.cs
  1. namespace WebAPIXMLJson  
  2. {  
  3.     public class Employee  
  4.     {  
  5.         public int Id { getset; }  
  6.         public string Name { getset; }  
  7.         public string City { getset; }   
  8.     }  
  9. }  
We can create an EmployeesController API class.
 
EmployeesController.cs
  1. using Microsoft.AspNetCore.Mvc;  
  2. using System.Collections.Generic;  
  3.   
  4. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860  
  5.   
  6. namespace WebAPIXMLJson.Controllers  
  7. {  
  8.     [Route("api/[controller]")]  
  9.     public class EmployeesController : Controller  
  10.     {  
  11.         [HttpGet("get.{format}"), FormatFilter]  
  12.         public IEnumerable<Employee> Get()  
  13.         {  
  14.             List<Employee> employees = new List<Employee>  
  15.             {  
  16.                 new Employee { Id = 1, Name = "Sarath Lal",City="Cochin" },  
  17.                 new Employee { Id = 2, Name = "Anil Kumar",City="Bangalore"}  
  18.             };  
  19.             return employees;  
  20.         }  
  21.   
  22.         [HttpPost("post.{format}"), FormatFilter]  
  23.         public Employee Post([FromBody]Employee employee)  
  24.         {  
  25.             if (ModelState.IsValid)  
  26.             {  
  27.                 return employee;  
  28.             }  
  29.             else  
  30.             {  
  31.                 return null;  
  32.             }  
  33.         }  
  34.     }  
  35. }  
Replace Employee Controller code with above code snippet.
 
 
We have decorated HttpGet and HttpPost with .{format} and also with “FormatFilter”.
 
You must pass the response type as XML or Json in every requests.
 
We can check the application in Postman tool.
 
We can easily get the data in XML format
 
 
We have added .xml at the end of request.
 
We can get the data in Json format in the same way.
 
 
If you send the request without any format like “http://localhost:52737/api/employees/get” you will get 404, not found error.
 
We can pass the Employee data as XML format in Post method also using Postman.
 
 
 
I have chosen the raw data format and XML type in Postman. Please note the request format is json. So, that we have received the data in json format though we have sent the data as XML format. We must carefully pass the employee data model namespace in XML namespace. Otherwise, data will not pass correctly to Web API method.

Conclusion


In this simple post, we have seen how to get data from Web API as XML and Json format in same web request. We have also seen how to pass data as XML and JSON in same Post request as well. There are various ways to achieve this result. I found that, this is one of the easiest ways. If you have got any other easiest way, please feel free to write as a comment.