Cannot implicitly convert Web.Http.Results.JsonResult to Web.Mvc.JsonResult
Asked Answered
M

6

11

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'

Markham answered 6/6, 2014 at 6:16 Comment(2)
Note how the immediate problem has nothing to do with anonymous types.Collenecollet
Json(object data) method that returns desired System.Web.Mvc.JsonResult is protected method of System.Web.Mvc.Controller. You need to inherit from Controller class to be able to use that. If your controller inherits from (eg) ApiController (as in my case;-) you are using Json<T>(T content) method that returns System.Web.Http.Results.JsonResult<T>...Idola
T
12

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;
    }
Terhune answered 6/6, 2014 at 6:21 Comment(0)
C
2

Try the following:

public System.Web.Http.Results.JsonResult test() 
{
    return Json(new { id = 1 });
}

It seems that Json does not generate a System.Web.Mvc.JsonResult which is expected as you are probably using System.Web.Mvc; but a System.Web.Http.Results.JsonResult.
The more generic one should also work:

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

NOTE:
In my MVC controllers the Json method does return a System.Web.Mvc.JsonResult. Are you inheriting from the default System.Web.Mvc.Controller?

Cartesian answered 6/6, 2014 at 6:17 Comment(1)
@ToanNguyen: Yes and no. For me the initial code is working, as Json does return a System.Web.Mvc.JsonResult in my controllers. But according to the exception it does not in his case...Cartesian
C
1

Try

return Json(new { id = 1 }, JsonRequestBehavior.AllowGet);

Cadel answered 6/6, 2014 at 6:18 Comment(0)
R
1

You need to return the data through a model class rather than an anonymous class. Like:

public System.Web.Http.Results.JsonResult<modelClass> test(){
        return Json(new modelClass(){ id=1 });
}
Rangy answered 3/12, 2014 at 10:45 Comment(0)
S
0

In MVC JsonResult is inherited from ActionResult which is in namespace System.Web.Mvc

thats why you should make the Reference to System.Web.Mvc.JsonResult as::

public System.Web.Mvc.JsonResult test() 
{
    return Json(new { id = 1 });
}
Stereometry answered 6/6, 2014 at 6:20 Comment(0)
K
0

Put this in your Using:

using System.Web.Http.Results;

Then Your Action:

public JsonResult<YourClass> Get(string Search)
        {
           var Search = Search
           return Json(Search);
        }
Kisner answered 10/7, 2016 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.