I have an enum with EnumMemberAttribute specified for its values:
public enum HireStrategy
{
[EnumMember(Value = "30-days")]
Days30,
[EnumMember(Value = "60-days")]
Days60,
[EnumMember(Value = "90-days")]
Days90
}
I want to be able to use the values 30-days
, 60-days
, 90-days
as input parameters for my api.
Here is services configuration:
services
.AddControllers()
.AddNewtonsoftJson(config =>
{
config.SerializerSettings.Converters.Add(new StringEnumConverter(typeof(CamelCaseNamingStrategy)));
});
services
.AddMvc();
And my action:
[HttpPost("test")]
public async Task<IActionResult> Test(HireStrategy type)
{
return Ok();
}
However, when I try to use the request (via postman)
https://localhost:44329/api/v1/users/test?type=60-days
I get validation error, i.e. it doesn't even hit my action:
{
"errors": {
"type": [
"The value '60-days' is not valid."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|b5f15582-4dc901ce029dfb30."
}
When I pass "days60" as value, it works fine. Is there anything I'm missing? Thanks!
UPDATE #1: I tried to disable ApiController validation to see if newtonsoft handle request:
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
But the parameter gets mapped to default value, which is Days30