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

问题描述

在我的ASP.NET Core(.NET Framework)项目中,我遇到了下面的Controller Action方法上的错误.我可能会缺少什么?或者,有什么解决方法吗?:

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的可接受的响应中,动作方法does notreturn type特别需要为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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 02:34
查看更多