[英]How to make a POST request with postman?
我有兩個模型,Sever 和 Update。 這兩個類之間的關系是 1...*N,這意味着每個 Server 都有多個更新。 這是我的服務器模型:
public class Server
{
public Server()
{
UpdateList = new HashSet<Update>();
}
[Key]
[Required]
public int ID { get; set; }
[Required]
public string ComputerName { get; set; }
[Required]
public string Type { get; set; }
[Required]
public string Estado { get; set; }
[Required]
public string Phase { get; set; }
public virtual ICollection<Update> UpdateList { get; set; }
}
這是我的更新模型:
public class Update
{
[Required]
public int ID { get; set; }
public string updateId { get; set; }
public string updateTitle { get; set; }
public string updateDescription { get; set; }
[System.ComponentModel.DataAnnotations.Schema.ForeignKey("Server")]
[Display(Name = "Server")]
public int? ServerFK { get; set; }
public virtual Server Server { get; set; }
}
這是我在郵遞員中使用的 POST 請求:
{
"computerName":"ServerTestUpdate",
"type":"ServidorAplicacional",
"estado":"Reboot Pending",
"phase":"0",
"updateList":
[{
"updateId":"idTest",
"updateTitle":"Update1",
"updateDescription":"TestDescription"
}]
}
但是,當我這樣做時,我收到以下錯誤:
System.Text.Json.JsonException: 檢測到可能的對象循環。 這可能是由於循環或對象深度大於最大允許深度 32。考慮在 JsonSerializerOptions 上使用 ReferenceHandler.Preserve 以支持循環。
語法有問題還是控制器中有問題?
先感謝您
這很可能是序列化對象的問題,因為反序列化不關心循環。 這可能是因為您的Server
對象有一組Update
,所有這些都有一個Server
屬性指向后,這會導致一個循環。
如果您將JsonIgnore
屬性添加JsonIgnore
屬性中,這應該會消失(來自System.Text.Json.Serialization
命名空間)
[JsonIgnore]
public virtual Server Server { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.