I\'m using ApiController and I can\'t get the call to return anything other than XML.
public class GuideController : ApiController
{
[AcceptVerbs(\"GET\")]
An easy way to make sure the api only returns JSON is to remove the xml formatter from the http configuration.
You can access the formatters in the WebApiConfig class
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//Clear current formatters
config.Formatters.Clear();
//Add only a json formatter
config.Formatters.Add(new JsonMediaTypeFormatter());
}
}
UPDATE
Don't serialize the object in the controller. Just return the object as is. Web api will do that for you as you have attached the json formatter to the configuration.
public class GuideController : ApiController
{
[AcceptVerbs("GET")]
[HttpGet]
public IHttpActionResult Get()
{
Item item = Item.GetTestData();
return Ok(item);
}
}
Try setting the Accept header in the client. If you want to receive JSON, set
Accept: application/json
in your client. Hope that helps.
Try to return JSON explicitly.
[HttpGet]
public IHttpActionResult Get()
{
Item item = Item.GetTestData();
return Json(item);
}