模型验证 - 为什么ModelState.IsValid总是返回true,即使不提供任何价值? [英] Model validation - Why ModelState.IsValid always returns true even when no values are supplied?
问题描述
这是关于的WebAPI
和
以下是我的Model类。
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; }
}
和我的控制器功能(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();
}
}
和我与每个请求一起提交的XML。
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>
在上面的XML post数据,我不为性别
可言,它被标记为 [必需] $提供一个值C $ C>。
In the above XML post data, I do not supply a value for Gender
at all, which are marked as [required]
.
但 ModelState.IsValid
返回true,即使在上面的XML是没有价值
为性别提供
。
But the ModelState.IsValid
returns true, even when in the above XML there is no value
supplied for Gender
.
如何从模型中的分配默认值一个枚举prevent的WebAPI?
How to prevent WebAPI from assigning default values to a enum in the model ?
任何想法,为什么?
推荐答案
我不知道为什么,如果你不提供性别你的模型是有效的,但你可以通过定义性别做出这个值没有默认值值可为空,如下所示:
I don't know why your model is valid if you don't supply gender, but you can make this value not have a default value by defining the Gender value as nullable, as follows:
public class Request
{
public int id { get; set; }
public string Name { get; set; }
[Required]
public Gender? Gender { get; set; }
}
另外,您可以指定性别的默认值,如下所示:
Alternatively you can specify a default value for gender, as follows:
public enum Gender
{
Unknown = 0,
Male,
Female
}
更新
我现在可以看到我们的结果之间的差异,再次使用<一个href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en\"相对=nofollow>邮差,如果我提交原始请求为XML:
Update I now can see the difference between our results, again using Postman, if I submit a raw request as xml:
标题:内容类型text / xml的
Header: Content-Type text/xml
<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>
我的模型是有效的,但如果我提交的X WWW的形式urlen codeD数据:
My model is valid, however if I submit x-www-form-urlencoded data:
标题:内容类型application / x-WWW的形式urlen codeD
Header: Content-Type application/x-www-form-urlencoded
id=1,Name=testname
然后我的模型是无效的,即使值类型有一个值,我的ModelState告诉我,性别属性是必需的。
Then my model is invalid, even though the value type has a value, my modelstate tells me that the Gender property is required.
为X-WWW的形式urlen codeD是查询字符串的一部分,那么我想这MVC是能够确定的价值不见了,但是当数据被提交为纯XML无法确定这一点。
As x-www-form-urlencoded is part of the query string then I guess that MVC is able to determine that the value was missing, but when the data is submitted as plain xml it can't determine this.
我建议,如果你想所需的属性在所有情况下工作,你让你的值类型可为空,如下所示:
I suggest that if you want the required attribute to work in all cases, you make your value types nullable as follows:
[Required]
public Gender? Gender { get; set; }
这篇关于模型验证 - 为什么ModelState.IsValid总是返回true,即使不提供任何价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!