在我的 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 的评论:
JsonRequestBehavior
在 ASP.NET Core 1.0 中已被弃用。- 在下面@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/