ASP. net mvc solves the JSON loop call problem, mvcjson
1. Newtonsoft. Json, an open-source Json serialization tool for. Net, provides a solution to the serialization cycle reference problem:
Method 1: Specify the Json serialization configuration as ReferenceLoopHandling. Ignore
Method 2: Specify JsonIgnore to ignore referenced objects
Example 1: Solve the Json serialization reference method of MVC:
Step 1: Add and reference the Newtonsoft. Json Package in the project. Command: Insert-Package Newtonsoft. Json
Step 2: Add a class to the project to inherit JsonResult. The Code is as follows:
/// <Summary >/// inherit JsonResut and rewrite the serialization method /// </summary> public class JsonNetResult: JsonResult {public JsonSerializerSettings Settings {get; private set ;} public JsonNetResult () {Settings = new JsonSerializerSettings {// This sentence is the key to solving the problem, that is, the configuration options officially provided by json.net. referenceLoopHandling = ReferenceLoopHandling. ignore };} public override void ExecuteResult (ControllerContext context) {if (context = null) thro W new ArgumentNullException ("context"); if (this. jsonRequestBehavior = JsonRequestBehavior. denyGet & string. equals (context. httpContext. request. httpMethod, "GET", StringComparison. ordinalIgnoreCase) throw new InvalidOperationException ("json get is not allowed"); HttpResponseBase response = context. httpContext. response; response. contentType = string. isNullOrEmpty (this. contentType )? "Application/json": this. ContentType; if (this. ContentEncoding! = Null) response. contentEncoding = this. contentEncoding; if (this. data = null) return; var scriptSerializer = JsonSerializer. create (this. settings); using (var sw = new StringWriter () {scriptSerializer. serialize (sw, this. data); response. write (sw. toString ());}}}
Step 3: Add BaseController to the project and rewrite the Json () method. The Code is as follows:
Public class BaseController: Controller {public StudentContext _ Context = new StudentContext (); // <summary> // rewrite, Json method, returns the JsonNetResult type /// </summary> protected override JsonResult Json (object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) {return new JsonNetResult {Data = data, contentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };}}
Step 4. Use it the same way as usual.
// Obtain the public JsonResult GetList () {List <student> list = _ Context. students. where (q => q. sno = "103 "). toList (); // method 1 return Json (list); // method 2 // return new JsonNetResult () {/Data = list //};}
Indicates whether to ignore the circular reference after the cyclic level is specified. The returned json data still contains partial cyclic data.
Solution: EF Json serialized circular reference method 2. Add the JsonIgnore method annotation to the specified correlated object.
[JsonIgnore]public virtual ICollection<score> scores { get; set; }
No table data is associated in the returned results.
Article Reprinted from: http://www.cnblogs.com/tianma3798/p/5596703.html