Cannot implicitly convert Web.Http.Results.JsonResult to Web.Mvc.JsonResult

Jhorra picture Jhorra · Jun 6, 2014 · Viewed 19.3k times · Source

I've set up this test method on a controller to strip out any complication to it. Based off of all the results I've found from searching this should work. I'm not sure what I'm missing here.

public JsonResult test() 
{
    return Json(new { id = 1 });
}

This is the error I get.

Cannot implicitly convert type 'System.Web.Http.Results.JsonResult' to 'System.Web.Mvc.JsonResult'

Answer

xei2k picture xei2k · Jun 6, 2014

you should return a JsonResult instead of just Json

 public JsonResult test() 
    {
        var result = new JsonResult();
        result.Data = new
        {
             id = 1
         };
        result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        return result;
    }