.NET MVC 表主外键关系 JSON 无限循环 方案二(推荐)
Posted 火焰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET MVC 表主外键关系 JSON 无限循环 方案二(推荐)相关的知识,希望对你有一定的参考价值。
public class JsonResultObject:JsonResult { private Newtonsoft.Json.JsonSerializerSettings Settings { get; private set; } public JsonResultObject() { Settings = new Newtonsoft.Json.JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }; } public override void ExecuteResult(ControllerContext context) { if (context == null) throw 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"); var 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 = Newtonsoft.Json.JsonSerializer.Create(this.Settings); using (var sw = new StringWriter()) { scriptSerializer.Serialize(sw, this.Data); response.Write(sw.ToString()); } } }
以上是关于.NET MVC 表主外键关系 JSON 无限循环 方案二(推荐)的主要内容,如果未能解决你的问题,请参考以下文章