码迷,mamicode.com
首页 > Web开发 > 详细

.NET MVC 表主外键关系 JSON 无限循环 方案二(推荐)

时间:2018-05-26 10:42:09      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:rri   type   void   writer   rip   result   opera   write   com   

   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 无限循环 方案二(推荐)

标签:rri   type   void   writer   rip   result   opera   write   com   

原文地址:https://www.cnblogs.com/valeb/p/9091522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!