ASP.NET核心-当前上下文中不存在名称“JsonRequestBehavior”

cnjp1d6j  于 2023-01-14  发布在  .NET
关注(0)|答案(5)|浏览(209)

在我的ASP.NET Core(. NET Framework)项目中,我在下面的控制器操作方法上遇到了上述错误。我可能遗漏了什么?或者,是否有任何变通方法?:

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核心1.0中已被弃用。
    1.在下面@Miguel接受的响应中,动作方法does notreturn type特别需要类型为JsonResult,ActionResult或IActionResult也可以。
3okqufwl

3okqufwl1#

返回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);
    }
}
disho6za

disho6za2#

在代码中,将To 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());
  }
}
bxjv4tth

bxjv4tth3#

有时候你需要用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 });
    }
}
qeeaahzv

qeeaahzv4#

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

return Json(countries, JsonRequestBehavior.AllowGet);

只管写

return Json(countries);

但是在 AJAX 的cshtml中,你应该用小写字母来调用你的实体属性,比如: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;
                }

            })
oxalkeyp

oxalkeyp5#

我一直在移植一个网站从asp.net到ASP.NET核心。我取代:return Json(data, JsonRequestBehavior.AllowGet);Json(data, new System.Text.Json.JsonSerializerOptions());和一切又开始工作。

相关问题