I have a complex type with many properties, including one which is an enum. When PUTing data (application/x-www-form-urlencoded) to a web API method which has this type as a parameter, it appears to allow ANY string value to be passed for the value for the enum property. If the value passed is one of the members of the enum, it correctly assigns the value, but if an invalid value is passed, it simply assigns the first member of the enumeration.
Simple example to describe the problem - given the model classes:
public enum EdibleFarmAnimal {
Sheep = 0,
Cow = 1,
Chicken = 2
}
public class ExampleModel {
public EdibleFarmAnimal EatThis { get; set; }
public string AnotherIrrelevantProperty { get; set; }
}
... and the web API method:
[HttpPut]
[ActionName("Put")]
public void Put(long id, ExampleModel model) {
// Do something with the model
}
If I PUT EatThis=Cow&AnotherIrrelevantProperty=cheese
to the relevant URL, it works as expected, and model.EatThis is equal to EdibleFarmAnimal.Cow, however if I PUT EatThis=Horse&AnotherIrrelevantProperty=cheese
then model.EatThis is set to EdibleFarmAnimal.Sheep, whereas I would like (and expect) an error of some sort to be thrown, as the input is not valid for the type it is being deserialised into.