【问题标题】:JsonProperty(Order = X)] is not working with [FromBody] and [FromRoute]JsonProperty(Order = X)] 不适用于 [FromBody] 和 [FromRoute]
【发布时间】:2020-12-21 01:01:38
【问题描述】:

在序列化版本中,它返回 Request 作为最后一个对象,即使我已经先订购了它。有没有办法设置请求订单1?

有没有什么,FromBody 会一直到最后?

    public class Class1
    {
        [FromRoute(Name = "runId")]
        [JsonProperty(Order = 2)]
        public string Id { get; set; }


        [FromBody]
        [JsonProperty(Order = 1)]
        public Request Request { get; set; }
    }

【问题讨论】:

    标签: asp.net-core serialization json.net swagger swagger-2.0


    【解决方案1】:

    Model Class1的绑定顺序是由模型中属性的顺序决定的,而不是[FromRoute][FromBody]。所以如果你想先绑定[FromBody],你可以这样做。这里有一个demo :

    public class Class1
        {
            [FromBody]
            public Sample Sample { get; set; }
            [FromRoute(Name = "runId")]
            public int Id { get; set; }
          
        }
        public class Sample
        {
            public int Foo { get; set; }
            public string Name { get; set; }
        }
    

    控制器:

    [HttpPost("Create/{runId}")]
            public IActionResult Create(Class1 partner) {
                return Ok();
            }
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2017-09-23
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多