ASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?


本文摘自PHP中文网,作者零下一度,侵删。

1..Net开源Json序列化工具Newtonsoft.Json中提供了解决序列化的循环引用问题:

方式1:指定Json序列化配置为 ReferenceLoopHandling.Ignore

方式2:指定 JsonIgnore忽略 引用对象

实例1,解决MVC的Json序列化引用方法:

step1:在项目上添加引用 Newtonsoft.Json程序包,命令:Insert-Package Newtonsoft.Json

step2:在项目中添加一个类,继承JsonResult,代码如下:

复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/// <summary>/// 继承JsonResut,重写序列化方式/// </summary>public class JsonNetResult : JsonResult

{public JsonSerializerSettings Settings { get; private set; }public JsonNetResult()

    {

        Settings = new JsonSerializerSettings

        {//这句是解决问题的关键,也就是json.net官方给出的解决配置选项.                

            ReferenceLoopHandling = 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");

        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());

        }

    }

}

复制代码

step3:在项目添加BaseController,重写Json()方法,代码如下:

复制代码

1

2

3

4

5

6

7

8

9

10

11

12

public class BaseController : Controller

{public StudentContext _Context = new StudentContext();/// <summary>/// 重写,Json方法,使之返回JsonNetResult类型/// </summary>protected override JsonResult Json(object data, string contentType,

        Encoding contentEncoding, JsonRequestBehavior behavior)

    {return new JsonNetResult

        {

            Data = data,

            ContentType = contentType,

            ContentEncoding = contentEncoding,

            JsonRequestBehavior = behavior

        };

    }

}

复制代码

step4.向平时一样使用就可以了

复制代码

1

2

3

4

//获取列表public JsonResult GetList()

{

    List<student> list = _Context.students.Where(q => q.sno == "103").ToList();//方法1return Json(list);//方法2//return new JsonNetResult() {//    Data=list//};

}

复制代码

获取的结果,说明,这种方式指定忽略循环引用,是在指定循环级数后忽略,返回的json数据中还是有部分循环的数据

解决EF Json序列化循环引用方法2,在指定的关联对象上,添加JsonIgnore 方法注释

1

[JsonIgnore]public virtual ICollection<score> scores { get; set; }

返回结果中,没有关联表数据


文章转载自:

以上就是ASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?的详细内容!

相关阅读 >>

asp.net 后台添加css、js、meta标签

在iis上部署asp.net core项目的步骤

解析asp.net如何使用session

关于json result的实例代码

关于asp.net core网站在docker中运行的详解

解析webgrid - 非常有用的 asp.net web 帮助器

分享asp.net学习笔记(9)webpages图表

asp.net数据绑定控件详解(图文)

传播智客asp.net中级系列视频视频资料

asp.net实现简单数字验证码实例

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...