问题描述
我遇到一些问题,这些问题与使用.NET Core Web API和Entity Framework Core进行自我引用有关.添加时,我的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中的自引用循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!