ASP.NET Web API – How to Always Return JSON

asp.net-mvcasp.net-web-api

ASP.NET Web API does content negotiation by default – will return XML or JSON or other type based on the Accept header. I don't need / want this, is there a way (like an attribute or something) to tell Web API to always return JSON?

Best Answer

Clear all formatters and add Json formatter back.

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

EDIT

I added it to Global.asax inside Application_Start().