[英]Ignore property from XML serialization/deserialization in ASP.NET Core 2
我已將[XmlIgnore]添加到忽略的屬性。 沒用
模型:
public class PointOfInterest // Child model
{
public string Name { get; set; }
public int CityId { get; set; } // Foreign key
[JsonIgnore] // Used to avoid circular reference
[XmlIgnore]
public City City { get; set; } // Parent
}
將application / xml設置為Accept標頭:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc() // Add MVC middleware to DI
.AddMvcOptions(option => option
// Add application/xml as Accept Header
.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter()))
錯誤:
System.Runtime.Serialization.SerializationException:類型為'WebApiDemo.Models.PointOfInterest'的對象圖包含循環,如果未跟蹤引用,則無法序列化。 考慮使用IsReference屬性設置為true的DataContractAttribute。
根據錯誤消息,嘗試將DataContractAttribute
與IsReference
設置一起使用:
Consider using the DataContractAttribute with the IsReference property set to true.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.