如何在 .Net Core Web API 中停止自引用循环?

编程入门 行业动态 更新时间:2024-10-23 23:20:14
本文介绍了如何在 .Net Core Web API 中停止自引用循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一些问题,我猜这些问题与使用 .NET Core Web API 和 Entity Framework Core 的自引用有关.当我添加 .Includes 用于某些导航属性时,我的 Web API 开始卡住了.

I'm having some issues which I'm guessing are related to self-referencing using .NET Core Web API and Entity Framework Core. My Web API starting choking when I added .Includes for some navigation properties.

我在较旧的 Web API 中找到了似乎是一个解决方案,但我不知道如何为 .NET Core Web API 实现相同的功能(我仍处于早期学习阶段).

I found what appears to be a solution in the older Web API but I don't know how to implement the same thing for .NET Core Web API (I'm still in the early learning stages).

旧的解决方案是在 Global.asax 的 Application_Start() 中坚持这个:

The older solution was sticking this in the Application_Start() of the Global.asax:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;

我怀疑这是在 StartUp 的 ConfigureService() 方法中处理的,但我对此知之甚少.

I suspect this is handled in the StartUp's ConfigureService() method but I don't know much beyond there.

或者有更合适的方法来处理这个问题吗?

Or is there a more appropriate way to handle this issue?

推荐答案

好的...我终于找到了一些关于这个的参考资料.解决办法是:

Okay... I finally found some reference material on this. The solution is:

public void ConfigureServices(IServiceCollection services) { ... services.AddMvc() .AddJsonOptions( options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore ); ... }

我从这里

更多推荐

如何在 .Net Core Web API 中停止自引用循环?

本文发布于:2023-11-15 04:15:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Net   Core   Web   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!