EnumDataType()属性验证错误消息未显示 [英] EnumDataType() attribute validation error message not displaying

查看:386
本文介绍了EnumDataType()属性验证错误消息未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.net core 2.0 Web API中,我在模型属性上使用EnumDataType()验证属性.验证失败时,自定义错误消息为空.我不确定为什么会发生-

In my .net core 2.0 Web API I am using EnumDataType() validation attribute on my model property. When the validation fails the custom error message is empty. I am not sure why it is happening -

[EnumDataType(typeof(MyEnum), ErrorMessage = "Custom Error Message")]
public MyEnum MyEnumProp {get; set;}

我检查了其他具有[Required],[MinLength]的属性,所有属性均生成自定义错误消息.难道我做错了什么?还有其他方法吗?

I checked the other properties where I have [Required], [MinLength] and all are generating custom error messages. Am I doing something wrong? Is there any other approach?

推荐答案

在反序列化和验证阶段发现的错误之间经常混淆.

It's a frequent confusion between errors detected during deserialization and validation stages.

假设您有以下enum:

public enum MyEnum
{
    None,
    Value1,
    Value2
}

和以下模型:

public class TestModel
{
    [Required]
    public int? Id { get; set; }

    [EnumDataType(typeof(MyEnum), ErrorMessage = "Custom Error Message")]
    public MyEnum MyEnumProp { get; set; }
}

发布数据时:

{
  "Id": 123,
  "MyEnumProp": "UnexistingEnumValue"
}

该错误将在反序列化阶段发生(对于这种情况,在Json.NET中).反序列化器无法将字符串"UnexistingEnumValue"转换为MyEnum中的某些值. 在这种情况下,解串器将注册以下模型绑定错误:Requested value 'UnexistingEnumValue' was not found.

the error will happen during deserialization stage (in Json.NET for this case). Deserializer just has no way to convert string "UnexistingEnumValue" to some of values from MyEnum. In this case deserializer will register following model binding error: Requested value 'UnexistingEnumValue' was not found.

ModelState.IsValid将设置为false,但是MyEnumProp的值将保留为其默认值MyEnum.None.通过EnumDataType属性执行的验证将不会检测到任何错误,因为MyEnum.NoneMyEnum的有效值.这就是为什么您不会在ModelState错误中看到"Custom Error Message"的原因.

ModelState.IsValid will be set to false, however value of MyEnumProp will be left at its default value of MyEnum.None. Validation performed by EnumDataType attribute will not detect any error, because MyEnum.None is a valid value for MyEnum. That is why you will not see "Custom Error Message" in ModelState errors.

现在,如果您发布以下数据:

Now if you post the following data:

{
  "Id": 123,
  "MyEnumProp": 5
}

在反序列化阶段不会发生任何错误,因为即使没有太大意义,以下分配也是合法的:

No error will happen during deserialization stage, because following assignment is pretty legal even if it does not make much sense:

MyEnum v = (MyEnum)5;

因此解串器将不会检测到任何错误.但是,现在EnumDataType验证开始起作用.并且它检测到5不是MyEnum的有效值. ModelState.IsValid设置为false,并且注册了在EnumDataType.ErrorMessage中指定的错误消息("Custom Error Message").

So deserializer will not detect any errors. However now EnumDataType validation comes into play. And it detects that 5 is not a valid value for MyEnum. ModelState.IsValid is set to false and the error message specified in EnumDataType.ErrorMessage is registered ("Custom Error Message").

如果要针对反序列化和验证错误使用相同的自定义消息,则应提升到反序列化器(Json.NET)的级别,并为此目的使用其可扩展性点.

If you want to have the same custom message for both deserialization and validation errors, you should go up to the level of deserializer (Json.NET) and use its extensibility points for this purpose.

这篇关于EnumDataType()属性验证错误消息未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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