问题
IN MVC6 return Json(rows, JsonRequestBehavior.AllowGet); method is changed and not allowing to set JsonrequestBehavior. What is alternative in MVC6
回答1:
That overload of Json
method which takes JsonRequestBehavior does not exist in the aspnet core any more.
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);
}
Or even
public IList<string> GetJsonData()
{
var rows = new List<string> {"aa", "bb" };
return rows;
}
or using Ok
method and having IActionResult
as the return type.
public IActionResult GetJsonData()
{
var rows = new List<string> { "aa", "bb" };
return Ok(rows);
}
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.
回答2:
Try this
[HttpGet]
public JsonResult List()
{
var settings = new JsonSerializerSettings();
return Json(rows, settings);
}
回答3:
Try this
public JsonResult GetJsonData()
{
var data= //your list values
return Json(data);
}
回答4:
JsonRequestBehavior is deprecated from ASP.net core 1. Just use return Json();
来源:https://stackoverflow.com/questions/39517210/in-mvc6-return-jsonrows-jsonrequestbehavior-allowget-issue