Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET web api returning XML instead of JSON [duplicate]

I read that by default, Web API will return JSON Data but for some reason when creating an API, it returns XML instead of JSON.

public class CurrencyController : ApiController {     private CompanyDatabaseContext db = new CompanyDatabaseContext();      // GET api/Currency     public IEnumerable<Currency> GetCurrencies()     {         return db.Currencies.AsEnumerable();     } } 

I haven't modified anything out of the ordinary so I'm stumped

like image 915
clifford.duke Avatar asked Aug 16 '13 06:08

clifford.duke


1 Answers

if you modify your WebApiConfig as follows you'll get JSON by default.

public static class WebApiConfig {     public static void Register(HttpConfiguration config)     {         config.Routes.MapHttpRoute(             name: "DefaultApi",             routeTemplate: "api/{controller}/{id}",             defaults: new { id = RouteParameter.Optional }         );          var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");         config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);     } } 
like image 124
chamara Avatar answered Nov 09 '22 17:11

chamara