【问题标题】:ASP.NET Core - The name 'JsonRequestBehavior' does not exist in the current contextASP.NET Core - 当前上下文中不存在名称“JsonRequestBehavior”
【发布时间】:2016-07-25 22:38:28
【问题描述】:

在我的 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 的 cmets:

  1. JsonRequestBehavior 在 ASP.NET Core 1.0 中已被弃用。
  2. 在下面@Miguel 接受的响应中,动作方法does notreturn type 特别需要是JsonResult 类型。 ActionResult 或 IActionResult 也可以。

【问题讨论】:

  • 查看JsonRequestBehavior 的文档。 Namespace 是您需要在文件顶部的 using 语句之后放置的内容,而 Assembly 是您必须包含作为项目参考的内容.
  • @SamIam 感谢您提供 MSDN 链接。我正在使用 ASP.NET Core 1.0 (.NET Framework) 项目模板,当我搜索 Reference-->Add 对话框时,该模板似乎没有可用的 System.Web.MVC 程序集。有什么建议或解决方法吗?
  • @nam AFAIK, JsonRequestBehavior 已在 ASP.NET Core 1.0 中弃用。

标签: c# asp.net json asp.net-core visual-studio-2015


【解决方案1】:

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

【讨论】:

  • 它不需要返回类型为JsonResultActionResultIActionResult 也可以。
  • @NateBarbettini 谢谢。我已将您的 cmets 添加到我帖子的额外 UPDATE 部分。
  • 因为 JsonResult 实现了 ActionResult :)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-19
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 2021-10-04
      相关资源
      最近更新 更多