In this tutorial, you will learn how to work with different data format in one web api application using Asp.net core web api framework 3.1
First we create an api application using asp.net core framework that return json data by default, data can be directly consumed from angular, node or any other web interface.
So, here are the things you will be learning from this post
By default, whenever you create any wep api application using asp.net core framework, it will provide data in json format, you simply create and run the application, you will see the default get method like following code.
[Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "value1", "value2" }; } }
Or if you want to create any custom api with a get method, that will return a list of custom object, (like in following example we are returning a list of student object), the list finally create a json string, unless you explicitly define that you want to return xml format.
public class StudentController : ControllerBase { [HttpGet] public List<Student> Get() { List<Student> stuList = new List<Student>(); stuList.Add(new Student { StuId = 1, FirstName = "Ajit", LastName = "India", Email = "ajit@india.com" }); stuList.Add(new Student { StuId = 2, FirstName = "Archana", LastName = "India", Email = "archana@india.com" }); stuList.Add(new Student { StuId = 3, FirstName = "Richard", LastName = "UK", Email = "richard@uk.com" }); stuList.Add(new Student { StuId = 4, FirstName = "Roshni", LastName = "USA", Email = "roshni@uk.com" }); stuList.Add(new Student { StuId = 5, FirstName = "Richa", LastName = "USA", Email = "richa@usa.com" }); return stuList; } }
When you run the application, browser will return those values in form of json format for above two methods.
JSON is the default type for Asp.net Core Web API 3.1 framework.
The above code will return result like examples below, the default raw data will be in JSON string.
[{"StuId":1,"FirstName":"Ajit","LastName":"India","Email":"ajit@india.com","Mobile":null,"Address":null},{"StuId":2,"FirstName":"Archana","LastName":"India","Email":"archana@india.com","Mobile":null,"Address":null},{"StuId":3,"FirstName":"Richard","LastName":"UK","Email":"richard@uk.com","Mobile":null,"Address":null},{"StuId":4,"FirstName":"Roshni","LastName":"USA","Email":"roshni@uk.com","Mobile":null,"Address":null},{"StuId":5,"FirstName":"Richa","LastName":"USA","Email":"richa@usa.com","Mobile":null,"Address":null}]
We can define the format we want to return either in startup.cs file or in method attribute.
Now we see how to return data in xml format instead of JSON, as I said JSON is the default format , so we have to make some changes in startup configuration method, so the API can return a different format like XML.
To set XML serialization, we have to configure AddControllers()
method with AddXmlSerializerFormatters()
public void ConfigureServices(IServiceCollection services) { services.AddControllers (options => { options.RespectBrowserAcceptHeader = true; // false by default }) .AddXmlSerializerFormatters(); }
You must set the options.RespectBrowserAcceptHeader = true;
in AddControllers method configuration.
After adding .AddXmlSerializerFormatters();
method in startup configure services method
when you run the Api, it will give you all ouput in XML format.
Now, think like a situation where in one Web API application you have multiple APIs, and in one particular API you want some or all methods to return json format only, but you have already configured API format as XML in startup.cs file which will affect all the APIs in application to be in same defined format.
In such situation we can apply attribute on the api method like [Produces("application/json")]
, where we want some other format to be returned, here in this case we can set json format for one method in student api.
[HttpGet] [Produces("application/json")] public List<Student> Get() { List<Student> stuList = new List<Student>(); stuList.Add(new Student { StuId = 1, FirstName = "Ajit", LastName = "India", Email = "ajit@india.com" }); stuList.Add(new Student { StuId = 2, FirstName = "Archana", LastName = "India", Email = "archana@india.com" }); stuList.Add(new Student { StuId = 3, FirstName = "Richard", LastName = "UK", Email = "richard@uk.com" }); stuList.Add(new Student { StuId = 4, FirstName = "Roshni", LastName = "USA", Email = "roshni@uk.com" }); stuList.Add(new Student { StuId = 5, FirstName = "Richa", LastName = "USA", Email = "richa@usa.com" }); //return new string[] { "value1", "value2" }; return stuList; }
Now the above method will return only JSON format, though we have set XML format in global application level.
Learn more about how to work with format specific action results in web api.