This is regarding WEBAPI
and
the following is my Model class.
public class Request
{
public int Id { get; set; }
public string Name { get; set; }
[Required]
public Gender Gender { get; set; }
}
And my controller function (POST)
public class Values1Controller : ApiController
{
public IHttpActionResult Post([FromBody] Models.Request request)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
var gender = request.Gender;
var id = request.Id;
var name = request.Name;
// do some operations!
return Ok();
}
}
And the xml that I submit along with each request.
<Request xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/webapi1.Models">
<id>1</id>
<name>testName</name>
</Request>
In the above XML post data, I do not supply a value for Gender
at all, which are marked as [required]
.
But the ModelState.IsValid
returns true, even when in the above XML there is no value
supplied for Gender
.
How to prevent WebAPI from assigning default values to a enum in the model ?
Any ideas Why ?