zoukankan      html  css  js  c++  java
  • .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());
                    }  
                } 
            }
    

      

  • 相关阅读:
    nginx的rewrite指令
    springmvc上传图片《2》
    cors解决跨域
    java8的Streams
    vue基础篇---路由的实现《2》
    vue基础篇---vue组件《2》
    vue基础篇---watch监听
    ES6新特性
    ubuntu tftp 配置
    驱动程序分层分离概念--总线驱动设备模型
  • 原文地址:https://www.cnblogs.com/valeb/p/9091522.html
Copyright © 2011-2022 走看看