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
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With