IN MVC6 return Json(rows, JsonRequestBehavior.AllowGet) ISSUE

◇◆丶佛笑我妖孽 提交于 2019-12-10 18:21:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!