ASP.NET Core - 当前上下文中不存在名称为 'JsonRequestBehavior' 的内容

71
在我的ASP.NET Core (.NET Framework)项目中,在以下Controller Action方法中出现上述错误。我可能漏掉了什么?或者,有没有任何解决方法?:
public class ClientController : Controller
{
    public ActionResult CountryLookup()
    {
        var countries = new List<SearchTypeAheadEntity>
        {
            new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
            new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
        };
        
        return Json(countries, JsonRequestBehavior.AllowGet);
    }
}

更新:

请注意以下@NateBarbettini的评论:

  1. JsonRequestBehavior 在ASP.NET Core 1.0中已被弃用。
  2. 在下面Miguel接受的响应中,操作方法的返回类型不一定要是JsonResult类型。ActionResult或IActionResult也可以。

请查看JsonRequestBehavior的文档。__命名空间__是您需要在文件顶部的using语句后面放置的内容,而__程序集__则是您必须包含到项目中作为引用的内容。 - Sam I am says Reinstate Monica
@SamIam 谢谢你提供的 MSDN 链接。我正在使用 ASP.NET Core 1.0 (.NET Framework) 项目模板,但在搜索 Reference-->Add 对话框时似乎没有 System.Web.MVC 程序集可用。有什么建议或解决方法吗? - nam
12
据我所知,JsonRequestBehavior 在 ASP.NET Core 1.0 中已被弃用。 - Nate Barbettini
5个回答

62

返回 JSON 格式的数据:

public class ClientController : Controller
{
    public JsonResult CountryLookup()
    {
         var countries = new List<SearchTypeAheadEntity>
         {
             new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
             new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
         };

         return Json(countries);
    }
}

8
不一定需要返回类型为 JsonResultActionResultIActionResult 也可行。 - Nate Barbettini
@NateBarbettini 谢谢你。我已经在我的帖子中添加了一个额外的“更新”部分,包含了你的评论。 - nam
因为JsonResult实现了ActionResult :) - Muflix

11

在代码中,将JsonRequestBehavior.AllowGet替换为new Newtonsoft.Json.JsonSerializerSettings()

它的作用与JsonRequestBehavior.AllowGet相同。

public class ClientController : Controller
{
  public ActionResult CountryLookup()
  {
    var countries = new List<SearchTypeAheadEntity>
        {
            new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
            new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
        };

    return Json(countries, new Newtonsoft.Json.JsonSerializerSettings());
  }
}

4

有时候需要返回JSON格式的消息,只需按照以下方式使用JSON结果,不再需要jsonrequestbehavior,下面是使用的简单代码:

public ActionResult DeleteSelected([FromBody]List<string> ids)
{
    try
    {
        if (ids != null && ids.Count > 0)
        {
            foreach (var id in ids)
            {
                bool done = new tblCodesVM().Delete(Convert.ToInt32(id));
                
            }
            return Json(new { success = true, responseText = "Deleted Scussefully" });

        }
        return Json(new { success = false, responseText = "Nothing Selected" });
    }
    catch (Exception dex)
    {
        
        return Json(new { success = false, responseText = dex.Message });
    }
}

0
我一直在将一个网站从asp.net迁移到asp.net Core。我用以下代码替换了原来的代码: return Json(data, JsonRequestBehavior.AllowGet); 替换为 Json(data, new System.Text.Json.JsonSerializerOptions()); 然后一切都重新开始工作了。

0

你好,在控制器中作为接受的答案,你不必说

return Json(countries, JsonRequestBehavior.AllowGet);

只需编写

return Json(countries);

但是在cshtml中的ajax中,您应该以小写字母开头调用实体属性,例如:shortCode和name。

$.ajax({
                method: "GET",
                url: `/ClientController/CountryLookup`
            }).done(function (result) {
                for (var i = 0; i < result.length; i++) {
                        var shortCode=result[i].shortCode;
                        var name= result[i].name;
                }

            })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接