【问题标题】:How to make a POST request with postman?如何使用邮递员发出 POST 请求?
【发布时间】:2021-12-14 11:51:47
【问题描述】:

我有两个模型,服务器和更新。这两个类之间的关系是 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 属性,因为它会指向父级,该父级指向该父级,该父级指向该父级...我假设这来自序列化,而不是反序列化。
  • 我该怎么做?

标签: json asp.net-core asp.net-mvc-4 post postman


【解决方案1】:

这很可能是序列化对象的问题,因为反序列化不会关心循环。这可能是因为你的Server 对象有一个Updates 的集合,所有这些都有一个Server 属性指向回来,这会导致一个循环。

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

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

【讨论】:

  • 就是这样,非常感谢
猜你喜欢
  • 1970-01-01
  • 2020-11-15
  • 2018-07-16
  • 2017-12-30
  • 2019-07-24
  • 2017-10-29
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多