使用新的 WebAPI 2.0 RC1 预发布位......给定这个方法:
[HttpPut("{sampleForm}/{id?}")]
public HttpResponseMessage PutSampleForm(SampleForm sampleForm, int? id)
{
if (!ModelState.IsValid)
{
// handle invalid model
}
// Insert valid model into DB with EF
return Request.CreateResponse(HttpStatusCode.OK);
}
它标有可为空的 id,但如果 id 实际上为空,则 ModelState 也被标记为无效......这是预期的,还是我可以做些什么让 ModelState 知道它需要忽略可为空的参数?