4

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

Rahul Sharma
  • 73
  • 3
  • 6
  • I am retruning json reuslt form my controller. it works with mvc4 by setting sonRequestBehavior.AllowGet .In MVC6 i am not able to set this beahviour... – Rahul Sharma Sep 15 '16 at 17:41

4 Answers4

7

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.

Shyju
  • 214,206
  • 104
  • 411
  • 497
4

Try this

 [HttpGet]
    public JsonResult List()
    {          
        var settings = new JsonSerializerSettings();

        return Json(rows, settings);
    }
Moro
  • 63
  • 1
  • 8
0

Try this

public JsonResult GetJsonData()
{
  var data= //your list values
  return Json(data);
}
JRA
  • 467
  • 5
  • 18
0

JsonRequestBehavior is deprecated from ASP.net core 1. Just use return Json();

Abdus Salam Azad
  • 5,087
  • 46
  • 35