If you’ve used Asp MVC Web Api then you are most likely familiar with the notion of content negotiation, this is the process where the content returned in the response is dictated by the accepts header in the request. In sort if you request xml you get xml back, if you request json you get json back.
This is done by what we call Formatters. You can of course add your own formatters, e.g. let’s say you have an application that returns human resource details; you may request back a profile picture by hitting the same route with a different request header.
Lets take a working example, of what we get out of the box (OOB).
Create a new Web Api project
Add the following class
Modify the ValuesController as follows
We now should be able to run the project and see the following in the browser
So by default we get back an xml formatted response, of course we could request json, but what if we just don’t want xml?
Tweaking the config
Add the following line to your Global.asax.cs
Now add this new GlobalConfig static class as follows
Run your application again
Now we get json by default, yah!
Run project again
e.g some simple jQuery