Controller.json 设置 Serialization.ReferenceLoopHandling [英] Controller.json set Serialization.ReferenceLoopHandling
本文介绍了Controller.json 设置 Serialization.ReferenceLoopHandling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法设置 Controller.Json ReferenceLoopHandling 属性?
is there a way to set Controller.Json ReferenceLoopHandling property?
在解析两端定义导航属性的实体时,当前会导致自引用循环.这个问题通过设置解决了
It is currently causing a self referencing loop when parsing entities with navigation properties defined on both ends. This problem is solved by setting
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
有没有办法为 Controller.Json 方法做到这一点?
Is there a way to do this for Controller.Json method?
我找到了这段代码,但它似乎不起作用.
I found this piece of code, but it does not seem to work.
services.Configure<MvcOptions>(option =>
{
option.OutputFormatters.Clear();
var jsonOutputFormatter = new JsonOutputFormatter();
jsonOutputFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
option.OutputFormatters.Insert(0, jsonOutputFormatter);
});
推荐答案
我认为一个更漂亮的解决方案是在您的 ConfigureServices 中添加 JsonOptions,例如:
I think that a prettier solution for this is to add JsonOptions in your ConfigureServices like:
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
这篇关于Controller.json 设置 Serialization.ReferenceLoopHandling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文