Short Circuit Model Validation C#

淺唱寂寞╮ 提交于 2019-12-11 17:58:54

问题


I'm using .NET Core 2.0. I have a POST action on my controller. It binds the data from the body of the request (JSON) into a C# DTO. On the DTO I have validation attributes - for instance:

[Required]
[MaxLength(512)]
public string email { get; set; }

These validators execute in the order that they are defined on the property.

I would like to short-circuit the validation if a prior validator fails. For example - if the required validation fails, don't execute the max length validation.

Is there a way to override the default model validation behavior to continue to the next property if one of the validators fails?

来源:https://stackoverflow.com/questions/48937926/short-circuit-model-validation-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!