How to return JSON in an ApiController for a single method?

If you can’t make a global change to force responses as JSON,
then try:

[Route("api/Player/videos")]
public HttpResponseMessage GetVideoMappings()
{
    var model = new MyCarModel();
    return Request.CreateResponse(HttpStatusCode.OK,model,Configuration.Formatters.JsonFormatter);
}

OR

[Route("api/Player/videos")]
public IHttpActionResult GetVideoMappings()
{
    var model = new MyCarModel();
    return Json(model);    
}

If you want to change globally, then first go to YourProject/App_Start/WebApiConfig.cs and add:

config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(
config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"));

at the bottom of the Register method.

Then try:

[Route("api/Player/videos")]
public IHttpActionResult GetVideoMappings()
{
    var model = new MyCarModel();
    return Ok(model);    
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)