Nainesh Patel
Nainesh Patel

Reputation: 598

JsonProperty(Order = X)] is not working with [FromBody] and [FromRoute]

In the serialized version, It returns Request as last object even though I have ordered it first. Is there a way to set Request order 1?

Is there anything like, FromBody will always be in the end?

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


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

Upvotes: 0

Views: 522

Answers (1)

Yiyi You
Yiyi You

Reputation: 18189

Model Binding order of Class1 is decided by the order of properties in the model,not by [FromRoute] or[FromBody].So if you want to bind [FromBody] first,you can do like this.Here is a 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; }
    }

Controller:

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

result: enter image description here

Upvotes: 1

Related Questions