ASP.NET MVC 5 – Return Restful JSON from Web Controller as Similar as Web API Controller Practice

ASP.NET MVC 5 – Return Restful JSON from Web Controller as Similar as Web API Controller Practice

 
   Controller
 
   public class PersonController : Controller
   {
     [HttpPost]
     public ActionResult TestJSON(List<Person> obj)
     {
       return Json(obj);
     }
 
     public class Person
     {
       public int ID { get; set; }
       public string Name { get; set; }
     }
   }
 
   AJAX JavaScript Code from Client Side
 
   data = [ { ID: 1, Name: "Name" }, { ID: 2, Name: "Name" }, … … ];
 
   $.ajax({
     url: "/Person/TestJSON",
     data: JSON.stringify(data),
     type: "POST",
     dataType: "json",
     contentType: "application/json",
     success: function (data) {
       alert(JSON.stringify(data));
     },
     error: function (e) {
       $("#exception").show();
       $("#exception").html(JSON.parse(e.responseText).Message + " – " + JSON.parse(e.responseText).ExceptionMessage);
     }
   });
 
   This Practice seems different from Web API with JSON.NET.
   It is able to pass Generic JSON Object from Client Side AJAX to Server Restful Web API Controller by using JSON.NET JObject.
   From ASP.NET MVC, The Class Type of Restful HttpRequest Parameter needs to be defined.
   FormCollection can also be as a Restful HttpRequest Parameter,
   but it is also different from Web API with JSON.NET Practice.
   A Set of Different Class Object from a JSON Array can be passed to Web API Controller with JSON.NET as a JObject Form Post Parameter.
   For Example, =>
   { "Data" :
   [
      { "Type" : "Person", Name : "Name", "Position" : "Boss" },
      { "Type" : "Company", "Name" : "ABC Company", "BRNum" : "A123456" },
      { "Type" : "Issue", "Title" : "… …", "Desc" : "… …", "Event Date" : "31/10/2017" },
   ]
   }
 
   It is possible to have the Other Library which can meet this Design from ASP.NET MVC Controller.
   The JSON Object can also be passed in Raw String Format from Client Side JavaScript to Controller.
 
   Sometimes, the Definition of Controller is based on the Module Functionality.
   Actually, the Design is expected to be separated
   the same System Module Functionality Web Feature & Restful Feature into Different Controller.