I have a Model object with a required attribute
public class ApiPing
{
[Required]
public DateTime ClientTime { get; set; }
public DateTime ServerTime { get; set; }
}
I have a Controller method that checks model state.
public IHttpActionResult Ping(ApiPing model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
model.ServerTime = DateTime.UtcNow;
return Ok(model);
}
If I submit a submit a proper request (with a model) to the action method I get an correct value from ModeState.IsValid (true). However, when I submit an invalid request (without a model, so the model is null) I get an erroneous ModelState.IsValid (also true).
I could simply check if the model is null in my code, but that smells. Is this an intended 'feature' or a bug in ModelState validation? Am I doing something wrong ? Am I expecting too much ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…