无法将Web.Http.Results.JsonResult隐式转换为Web.Mvc.JsonResult [英] Cannot implicitly convert Web.Http.Results.JsonResult to Web.Mvc.JsonResult

查看:244
本文介绍了无法将Web.Http.Results.JsonResult隐式转换为Web.Mvc.JsonResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在控制器上设置了此测试方法,以消除任何复杂性.根据我从搜索中发现的所有结果,该方法应该可以工作.我不确定我在这里想念什么.

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 });
}

这是我得到的错误.

不能将类型'System.Web.Http.Results.JsonResult'隐式转换为'System.Web.Mvc.JsonResult'

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

推荐答案

您应该返回JsonResult而不只是Json

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;
    }

这篇关于无法将Web.Http.Results.JsonResult隐式转换为Web.Mvc.JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆