如何向邮递员发出 POST 请求?

How to make a POST request with postman?

我有两个模型,服务器和更新。这两个类之间的关系是1...*N,也就是说每个Server都有多个update。 这是我的服务器型号:

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: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.

语法有问题还是控制器有问题?

提前致谢

这很可能是序列化对象时出现的问题,因为反序列化不关心循环。这可能是因为您的 Server 对象有一个 Update 的集合,所有这些集合都有一个 Server 属性 指向后方,这会导致一个循环。

如果您将 JsonIgnore 属性添加到 属性 这应该消失(来自 System.Text.Json.Serialization 命名空间)

[JsonIgnore]
public virtual Server Server { get; set; }