问题描述
我正在使用ASP.NET Core 2.0,并且我有一个这样注释的请求对象:
I'm using ASP.NET Core 2.0, and I have a request object annotated like this:
public class MyRequest
{
[Required]
public Guid Id { get; set; }
[Required]
public DateTime EndDateTimeUtc { get; set; }
[Required]
public DateTime StartDateTimeUtc { get; set; }
}
在我的控制器中:
public async Task<IActionResult> HandleRequest([FromBody] MyRequest request)
{ /* ... */ }
我注意到模型绑定的问题:当我发送包含标头Content-Type
设置为application/json
且为空主体的请求时,正如我所期望的那样,控制器中的request
是null
和ModelState.IsValid
是false
.
I noticed an issue with model binding: When I send a request containing the header Content-Type
set to application/json
and a an empty body, as I expect, the request
in my controller is null
and ModelState.IsValid
is false
.
但是当我有一个这样的身体时:
But when I have a body like this:
{
"hello": "a-string-value!"
}
我的request
不是null
,它具有所有内容的默认值,而ModelState.IsValid
是true
my request
is NOT null
, it has default values for everything, and ModelState.IsValid
is true
这当然是在我缺少所有Required
属性的情况下发生的,并且唯一存在的名称与那里的属性不匹配(即使该单个参数的类型是string
,也并非如此)匹配我模型上的任何类型.
This is happening of course while I'm missing all the Required
properties, and the only existing one's name doesn't match a property there (even the type for this single parameter is string
, which doesn't match any type on my model).
以某种方式,如果我的请求中没有任何内容,那些Required
属性似乎可以正常工作,但是如果我的请求不为空,则它们将不执行任何操作!
So in a way, those Required
attributes seem to be working if there's nothing in my request, but they don't do anything if my request is not empty!
在准备这个问题时,我注意到还有一个JsonRequired
属性,它似乎照顾了存在的属性.
As I was preparing this question, I noticed that there's also a JsonRequired
attribute, and it seems to take care of the properties being present.
那么Required
和JsonRequired
有什么区别?
推荐答案
要正确使用Required
属性,应将属性设置为可空:
For correct work of Required
attribute, you should make the properties nullable:
public class MyRequest
{
[Required]
public Guid? Id { get; set; }
[Required]
public DateTime? EndDateTimeUtc { get; set; }
[Required]
public DateTime? StartDateTimeUtc { get; set; }
}
现在,如果您发送缺少Id
,EndDateTimeUtc
或StartDateTimeUtc
的请求,则相应字段将设置为null
,ModelState.IsValid
将设置为false
,并且ModelState
将包含错误( s)说明,例如The EndDateTimeUtc field is required.
Now if you send request with missing Id
, EndDateTimeUtc
or StartDateTimeUtc
, corresponding field will be set to null
, ModelState.IsValid
will be set to false
and ModelState
will contain error(s) description, e.g. The EndDateTimeUtc field is required.
JsonRequired
属性特定于JSON.Net.它在反序列化期间播放,而Required
属性(与System.ComponentModel.DataAnnotations
命名空间中的其他属性一样)在模型验证后反序列化之后播放.如果违反了JsonRequired
属性,则该模型将完全不反序列化,并且相应的action参数将设置为null
.
JsonRequired
attribute is specific to JSON.Net. It plays during deserialization, while Required
attribute (as other attributes from System.ComponentModel.DataAnnotations
namespace) plays after model is deserialized, during model validation. If JsonRequired
attribute is violated, the model will not be deserialized at all and corresponding action parameter will be set to null
.
与JsonRequired
相比,更应该使用Required
属性的主要原因是JsonRequired
不适用于其他内容类型(例如XML). Required
反过来又是通用的,因为它是在反序列化模型之后应用的.
The main reason why you should prefer Required
attribute over JsonRequired
is that JsonRequired
will not work for other content types (like XML). Required
in its turn is universal since it's applied after the model is deserialized.
这篇关于在带有JSON正文的ASP.NET Core模型绑定中使用Required和JsonRequired的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!