在MVC6中返回Json(rows,JsonRequestBehavior.AllowGet)问题 [英] IN MVC6 return Json(rows, JsonRequestBehavior.AllowGet) ISSUE

查看:275
本文介绍了在MVC6中返回Json(rows,JsonRequestBehavior.AllowGet)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC6中返回Json(rows,JsonRequestBehavior.AllowGet);方法已更改,不允许设置JsonrequestBehavior. MVC6中的替代方法是什么

IN MVC6 return Json(rows, JsonRequestBehavior.AllowGet); method is changed and not allowing to set JsonrequestBehavior. What is alternative in MVC6

推荐答案

aspnet核心中不再存在采用JsonRequestBehavior的Json方法的重载.

That overload of Json method which takes JsonRequestBehavior does not exist in the aspnet core any more.

您只需使用要发送回的对象数据调用Json方法.

You can simply call the Json method with the object data you want to send back.

public IActionResult GetJsonData()
{
  var rows = new List<string>  {  "Item 1","Item 2" };
  return Json(rows);
}

甚至

public IList<string> GetJsonData()
{
    var rows = new List<string>  {"aa", "bb" };
    return rows;
}

或使用Ok方法并以IActionResult作为返回类型.

or using Ok method and having IActionResult as the return type.

public IActionResult GetJsonData()
{
   var rows = new List<string>   { "aa",  "bb"  };
    return Ok(rows);
}

,然后让内容协商器以请求的格式(通过Accept标头)返回数据. ASP.NET Core MVC使用的默认格式是JSON.因此,如果您未明确要求其他格式(例如:application/xml),则会得到json响应.

and let the content negotiator return the data in the requested format(via Accept header). The default format used by ASP.NET Core MVC is JSON. So if you are not explicitly requesting another format(ex :application/xml), you will get json response.

这篇关于在MVC6中返回Json(rows,JsonRequestBehavior.AllowGet)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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