相关疑难解决方法(0)

如何配置 ASP.NET Core 以处理循环引用而不破坏主体的响应?

我有这个 ASP.NET Core 2.0 MVC 控制器:

[Route("api/[controller]")]
public class SampleDataController : Controller
{
    [HttpGet("[action]")]
    public Example Demo()
    {
        return new Example("test");
    }

    public class Example
    {
        public Example(string name)
        {
            Name = name;
        }

        public string Name { get; }

        public IEnumerable<Example> Demos
        {
            get { yield return this; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

查询时/api/SampleData/Demo,我得到响应正文:

{"name":"test","demos":[
Run Code Online (Sandbox Code Playgroud)

...这显然是非常破碎的类似 JSON 的输出。

我必须如何以及在哪里配置基于 ASP.Net Core 2.0 MVC 的应用程序,以使框架以不破坏输出的方式序列化循环引用?(例如,通过引入$ref$id。)

asp.net asp.net-core-mvc asp.net-core asp.net-core-2.0

5
推荐指数
1
解决办法
2163
查看次数