在带有JSON正文的ASP.NET Core模型绑定中使用Required和JsonRequired [英] Using Required and JsonRequired in ASP.NET Core Model Binding with JSON body

查看:171
本文介绍了在带有JSON正文的ASP.NET Core模型绑定中使用Required和JsonRequired的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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且为空主体的请求时,正如我所期望的那样,控制器中的requestnullModelState.IsValidfalse.

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.IsValidtrue

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.

那么RequiredJsonRequired有什么区别?

推荐答案

要正确使用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; }
}

现在,如果您发送缺少IdEndDateTimeUtcStartDateTimeUtc的请求,则相应字段将设置为nullModelState.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆