locked
Enum model binding [FromRoute] behaves differently than [FromBody] in .net core RRS feed

  • Question

  • User-505785694 posted

    I am getting different behaviors in the bindind model with enum and I can't get the correct value when passing information that is out of the enumeration limit.

    I have a TestEnum enumeration containing Test1 = 1, Test2 = 2, Test3 = 3.

    In [HttpPost] or [HttpPut] using [FromBody], my DTO object correctly receives the enumeration value, even when I pass a different value like 5 for example.

    However, in a Get/Put using [FromRoute], my DTO object correctly receives enumeration when the entered values are within the enumeration limit (1, 2, or 3).

    If I pass 5, the value of the enumerator becomes zero, unlike [FromBody] which can receive the value 5 normally even though it is outside of the coded Test1, Test2, and Test3.

    public enum TestEnum
    {
       Test1 = 1,
       Test2 = 2,
       Test3 = 3
    }

    It perfectly receives the values even being outside the enumeration limit.

    [HttpPost]
    public async Task<ActionResult<NotificationResult>> Post([FromBody]TestCommand command)

    Here, in [FromRoute] and [FromBody] values are perfectly received when they are within the enumeration boundary.

    When out of range, [FromRoute] gets zero and [FromBody] gets 5.

    [HttpPut("{id:int}")]
    public async Task<ActionResult<NotificationResult>> Put([FromRoute]TestEnum id, [FromBody]TestCommand command)

    Here, in [FromRoute] values are perfectly received when they are within the enumeration boundary.

    When out of range, [FromRoute] gets zero.

    [HttpGet("{id:int}")]
    public async Task<ActionResult<TestQueryResult>> GetById([FromRoute]TestEnum id)
    

    I would like to receive the value even if it is outside the encoded limit in enumeration.

    Sunday, August 11, 2019 3:45 PM

Answers

  • User711641945 posted

    Hi Eduardo,

    The default value of options SuppressBindingUndefinedValueToEnumType is true after .net core 2.1. So you need to change like below:

    services.AddMvc(opt=>
    opt.SuppressBindingUndefinedValueToEnumType=false)
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2019 6:43 AM

All replies

  • User303363814 posted

    What is the definition of TestCommand? What does the body of the Post look like?

    Monday, August 12, 2019 7:38 AM
  • User-505785694 posted

    Hello.

    TestCommand is an object:

    public class TestCommand
    {
        public TestEnum Id {get; set; }
        public string Name {get; set; }
    }

    No problem here.

    What I meant is that when I call Put, Id gets 3 when: http://localhost/test/3 (inside the enum list)
    but Id gets 0 when http://localhost/test/5 (outside
    from the enum list).

    Monday, August 12, 2019 9:48 AM
  • User303363814 posted

    What does the body of the Post look like?

    Monday, August 12, 2019 11:11 PM
  • User711641945 posted

    Hi Eduardo,

    The default value of options SuppressBindingUndefinedValueToEnumType is true after .net core 2.1. So you need to change like below:

    services.AddMvc(opt=>
    opt.SuppressBindingUndefinedValueToEnumType=false)
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2019 6:43 AM
  • User-505785694 posted

    Wow.
    It worked.
    That was it.
    Thank you very much, Rena Ni.

    Tuesday, August 13, 2019 10:09 AM