ASP.NET MVC 5 Controller JsonResult – return JSON ( application/json ) 方法
public class JSONController : Controller { public JsonResult Index() { List<Test> list = new List<Test>(); list.Add(new Test(1, "Value 1", "Value 2")); list.Add(new Test(2, "Value 1", "Value 2")); list.Add(new Test(3, "Value 1", "Value 2")); list.Add(new Test(4, "Value 1", "Value 2")); return Json(list, "application/json", JsonRequestBehavior.AllowGet); } public class Test { public Test(int ID, string Attr1, string Attr2) { this.ID = ID; this.Attr1 = Attr1; this.Attr2 = Attr2; } public int ID { get; set; } public string Attr1 { get; set; } public string Attr2 { get; set; } } } |
Remark :
The Practice is as similar as ASP.NET MVC with JSON.NET ( return Data Type JObject or JArray ).
private JObject Get(…) / private JArray Get(…)