c# - ASP.NET Core - 当前上下文中不存在名称 'JsonRequestBehavior'

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

在我的 ASP.NET Core (.NET Framework) 项目中,我的以下 Controller 操作方法出现上述错误。我可能会错过什么?或者,有什么解决办法吗?:

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 接受的响应中, Action 方法返回类型 并不具体需要是JsonResult 类型。 ActionResult 或 IActionResult 也可以。

最佳答案

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

关于c# - ASP.NET Core - 当前上下文中不存在名称 'JsonRequestBehavior',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38578463/

相关文章:

c# - 如何让 FxCop 与 Sonar 一起使用 C# 解决方案?

c# - 如何将 System.Drawing.Icon 转换为 System.Drawing.Image?

c# - 当我在 asp.net mvc Controller 操作中验证失败时,如何保留我的 url

c# - 未创建身份表

使用 JAX-RS/RESTEasy 实现 CORS 的 Ajax 请求

c# - 避免 UserControl 捕获鼠标滚轮滚动

asp.net - 我们可以通过客户端浏览器看到 html localstorage 内容吗?

c# - 如何解决 "Failed to bind to address http://127.0.0.1:5000: address already in use."

javascript - 在谷歌地图中显示 JSON 响应(纬度、经度)

python - Pandas 到 D3。将数据帧序列化为 JSON