ASP.NET Core - 当前上下文中不存在名称“JsonRequestBehavior" [英] ASP.NET Core - The name 'JsonRequestBehavior' does not exist in the current context

查看:55
本文介绍了ASP.NET Core - 当前上下文中不存在名称“JsonRequestBehavior"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 ASP.NET Core (.NET Framework) 项目中,我在以下控制器操作方法中遇到上述错误.我可能缺少什么?或者,是否有任何解决方法?:

In my ASP.NET Core (.NET Framework) project, I'm getting above error on my following Controller Action method. What I may be missing? Or, are there any work arounds?:

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 的评论:

Please note folowing comments from @NateBarbettini below:

  1. JsonRequestBehavior 在 ASP.NET Core 1.0 中已被弃用.
  2. 在下面来自@Miguel 的接受响应中,操作方法不需要返回类型特别需要是JsonResult类型.ActionResult 或 IActionResult 也有效.
  1. JsonRequestBehavior has been deprecated in ASP.NET Core 1.0.
  2. In accepted response from @Miguel below, the return type of action method does not specifically need to be of type JsonResult. ActionResult or IActionResult works too.

推荐答案

返回 Json 格式的数据:

Returning Json-formatted data:

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

这篇关于ASP.NET Core - 当前上下文中不存在名称“JsonRequestBehavior"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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