字符串字段为必填。即使在Asp.Net Core中没有“必需”属性?

问题描述 投票:1回答:1

我正在linux(pop os)中构建一个简单的Asp.Net Core应用程序。我正在使用VueJs + Aps.Net Core 3.1.101我正在尝试对我的应用程序进行POST调用,而我的模型如下所示:

public class AddConfigurationContextValueApiRequest
{
    public int ContextId { get; set; }

    [Required(ErrorMessage = "Value is required to continue")]
    [StringLength(500, ErrorMessage = "Value can not be longer than 500 characters")]
    public string Value { get; set; }

    [StringLength(500, ErrorMessage = "Display name can not be longer than 500 characters")]
    public string DisplayName { get; set; } 
}

如您所见,Required字段没有DisplayName属性,但是每当我从VueJS应用程序为此字段传递null值时,都会得到The DisplayName field is required.

我试图弄清楚为什么AspNet Core会为此抱怨,因为该字段没有Required属性!

有人知道这是否是故意的吗?我试图删除StringLength属性,但仍会触发必需的属性。

我的动作非常简单:

[HttpPost(UrlPath + "addConfigurationContextValue")]
public async Task AddConfigurationContextValue([FromBody]AddConfigurationContextValueApiRequest request)
{
    using var unitOfWork = _unitOfWorkProvider.GetOrCreate();
    if (!ModelState.IsValid)
    {
        //Here it throws because ModelState is invalid
        throw new BadRequestException(ModelState.GetErrors());
    }

    //do stuff

    await unitOfWork.CommitAndCheckAsync();
}
c# asp.net-core asp.net-core-3.1
1个回答
0
投票

在@devNull的建议之后,我发现以某种方式在玩Rider IDE时似乎已打开该功能!

骑士中有一个选项可以在项目级别更改该配置:

如果有人遇到相同的问题:右键单击项目级别,然后转到属性,应用程序,然后您就可以看到此配置。

感谢@devNull提供帮助:)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.