ASP. net mvc solves the JSON loop call problem, mvcjson

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.