枚举错误时requestModel为null
如果您有此枚举:
public enum MyEnum
{
Value1,
Value2
}
并且此请求:
{
"myEnum" : "WrongValue",
"name" : "MyName"
}
在控制器中:
public async Task<ActionResult> RequestAsync(
[FromBody] MyRequest myRequest)
{
}
MyRequest为无效。
但是如果您使用;
{
"myEnum" : "Value1",
"name" : "MyName"
}
它不是零。
If you have this enum :
public enum MyEnum
{
Value1,
Value2
}
And this Request :
{
"myEnum" : "WrongValue",
"name" : "MyName"
}
In the controller :
public async Task<ActionResult> RequestAsync(
[FromBody] MyRequest myRequest)
{
}
myRequest is null.
But if you use ;
{
"myEnum" : "Value1",
"name" : "MyName"
}
It is not null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,当无效的枚举值传递给模型时,您将收到整个请求模型为null
,但我们可以利用
microsoft.aspnetcore.mvc.modelstate
检查请求模型是否有效,这将从
ModelState
用于响应中无效的枚举属性如下Yes correct when invalid Enum value passed to model, you will receive entire request model as null
but we can leverage
Microsoft.AspNetCore.Mvc.ModelState
to check whether request model is valid or notthis will return error from
ModelState
for invalid enum property in response as below