In an Asp.Net Web API controller I have, there is a PostAsync method that bind the request JSON body to a model class.
E.g.:
public class EmployeesController : ApiController
{
...
public async Task<HttpResponseMessage> PostAsync([FromBody] Employee employee)
{
...
}
...
}
The Employee model class contains a numeric property, Height:
public class Employee
{
...
public decimal Height { get; set; }
...
}
The problem is that when posting to the above controller action with request payload that contains a Height value with a leading zero, the FromBodyAttribute binder automatically converts the value to octal base, e.g.: 010 is translated as the value 8 in decimal base.
Sample request body:
{
...
"Height": 010,
...
}
How can I prevent the conversion to an octal base?
What I've tried so far:
- Implement a
PropertyBindAttribute
like described in this blog post, but the attribute receives the value post conversion to Octal base, so it's too late in the binding-life-cycle to use this approach - Convert the property to string type, and use a custom
ValidationAttribute
to validate the string is format match a numeric decimal base value, then converting the value to decimal and assigning it to another decimal property / backing-field