ASP.NET Web API: JSON Serializing Circular References(ASP.NET Web API:JSON 序列化循环引用)
问题描述
我正在通过 ASP.NET Web API 检索 JSON 对象图.我正在尝试从子实体访问属性.然而,当查看浏览器的控制台时,它显示的是对象的 reference ($ref
),而不是序列化该对象的属性.如何访问这些属性?
I am retrieving a JSON object graph via ASP.NET Web API. I'm trying to access the properties from a child entity. When looking at the browser's console, however, it is showing a reference ($ref
) to the object, instead of serializing the properties of that object. How do I get access to these properties?
角度 JS 视图
<table infinite-scroll='tF.loadMore()' infinite-scroll-disabled='tF.isBusy' infinite-scroll-distance='3' class="responsive">
<thead>
<tr>
<th>FIELD 1</th>
<th>FIELD 2</th>
<th>FIELD 3</th>
<th>FIELD 4</th>
<th>FIELD 5</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in tF.items | filter:searchFilter">
<td>{{item.CompanyDomainModel.CompanyName}}</td>
<td>{{item.RatingDomainModel.RatingValue}}</td>
<td>{{item.Views}}</td>
<td>{{item.Clicks}}</td>
<td>{{item.EmailSent}}</td>
</tr>
</tbody>
<tfoot ng-show='tF.isBusy'>
<tr>
<td colspan="9"><spinner show="tF.isBusy" /><span class="bold">{{tF.status}}</span> </td>
</tr>
</tfoot>
</table>
服务
public ICollection<CompanyStatDomainModel> GetRecordsByPageSize(int page) {
const int pgeSize = 20;
var result = _companyStatRepo
.AllIncluding(c => c.CompanyDomainModel, c => c.RatingDomainModel)
.OrderBy(c => c.CompanyStatId)
.Skip(page * pgeSize)
.Take(pgeSize)
.ToList();
return result;
}
端点
IHttpActionResult GetRecordsByPageSize(int page) {
var companyStatService = new CompanyStatService();
return Ok(companyStatService.GetRecordsByPageSize(page));
}
评级域模型
public class RatingDomainModel : IObjectWithState
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DataMember]
public int RatingId { get; set; }
[DataMember]
public int CompanyId { get; set; }
[DataMember]
public int UserId { get; set; }
[DataMember]
public int RatingValue { get; set; }
[DataMember]
public DateTime CreatedDate { get; set; }
//[ForeignKey("UserId")]
[DataMember]
public virtual UserDomainModel UserDomainModel { get; set; }
//[ForeignKey("CompanyId")]
[DataMember]
public virtual CompanyDomainModel CompanyDomainModel { get; set; }
[DataMember]
public virtual ICollection<CompanyStatDomainModel> CompanyStatDomainModels { get; set; }
[NotMapped]
public Common.DataObject.State state { get; set; }
[NotMapped]
public bool InDb
{
get { return this.RatingId != default(int); }
}
public object PersistenceEntityId
{
get { return this.RatingId; }
}
}
输出
推荐答案
在WebApiConfig.cs中添加如下代码
Added the below code into WebApiConfig.cs
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
这篇关于ASP.NET Web API:JSON 序列化循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET Web API:JSON 序列化循环引用
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01