Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify if I want JSON or XML in ASP.NET Web API?

I've written two ASP.NET Web API applications this week which both contain a single simple controller; I've tweaked each to get them to work, include exceptions, and so on but they are configured almost the same as far as I can see.

One of these returns JSON and the other returns XML. I want them both to return JSON!

I've looked for some configuration which might control this behaviour but I'm having no joy. Where is the setting which controls the serialization type used by the ASP.NET Web API?

like image 277
Kirk Broadhurst Avatar asked Aug 09 '12 23:08

Kirk Broadhurst


1 Answers

It is defined by what the calling client (eg the browser or your .NET client) passes in the Accept header:

Accept: application/json, application/xml, text/json, text/xml

Will have a preference for JSON (if possible)

So your client that returns XML needs to set the Accept header to be the above or simply

Accept: application/json

should do the trick

like image 148
wal Avatar answered Oct 11 '22 19:10

wal