I’m currently watching a video course about ASP.NET Web API. When a controller gets called, the data gets returned in JSON by default. I was just wondering, because when I copy this sample project from the video, I get XML.
The frustration is big, please help me to solve this.
I’m pretty new to ASP.NET Web API, so please bear with me.
UPDATE
The controller doesn’t contain special code. It’s the simple code, which gets generated from the API Controller with empty read/write actions template.
ASP.NET WebAPI comes with built-in content negotitation therefore the format of the return value is determined by the request itself – more specifically by the
Accept/Content-Typeheaders (depending on which ones are present,Acceptheader appears to be favoured over theContent-type).I assume you’re viewing the results in a browser and by default it’s probably asking for
application/xml. You will need to toy around with some settings/developer tools on the browser and force it to sendContent-Type: application/jsonto get the correct response (assuming your returning HttpResponseMessage).