gpt4 book ai didi

ASP.NET Web API : JSON Serializing Circular References

转载 作者:行者123 更新时间:2023-12-02 14:56:08 25 4
gpt4 key购买 nike

我正在通过 ASP.NET Web API 检索 JSON 对象图。我正在尝试从子实体访问属性。然而,当查看浏览器的控制台时,它显示的是对该对象的引用 ($ref),而不是序列化该对象的属性。我如何访问这些属性?

Angular JS View

<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; }
}
}

输出

Please see below URL to see the results I am getting from the API

最佳答案

将以下代码添加到 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 Serializing Circular References,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com