我似乎无法覆盖来自的模型状态验证的错误消息
int
或可为空的
int?
. 在Asp的早期版本中。Net Core我以前收到的输入无效,现在我收到了这个不友好的错误,
{“streetNo”:[“无法将字符串转换为整数:abc。路径‘cityId’,
第24行,位置23。"]}
所以我尝试使用自定义验证属性来解决这个问题,
我创建了这个类,
public class IsInt : ValidationAttribute {
public IsInt () : base () { }
public override bool IsValid (object value) {
Console.WriteLine (value);
if (value.IsNullObject ()) {
return true;
} else {
if (value.GetType () == typeof (int?)) {
return true;
} else {
return false;
}
}
}
protected override ValidationResult IsValid (
Object value,
ValidationContext validationContext) {
var message = "Only number is allowed";
return new ValidationResult (message);
}
}
我是这样实施的,
[IsInt]
public int? StreetNo { get; set; }
validation属性似乎没有按预期工作,如果我输入一个字符串,即“abc”,我仍然收到提到的模型错误消息,它只在字符串中有数字时工作,即“83444”
我错过了什么?