数据注释上的Email属性给出了两种不同的结果

本文关键字:两种 结果 注释 Email 属性 数据 | 更新日期: 2023-09-27 18:10:41

我得到奇怪的行为与电子邮件属性(数据注释扩展)。当我从CSV文件导入员工时,当该列为空时,电子邮件无效,但当我从表单创建员工时,即使我留下电子邮件为空,它也可以完美地工作。我不知道为什么这两个行为是相同的属性

数据注释上的Email属性给出了两种不同的结果

这是你的代码没有错,这是一个错误,根据我在电子邮件属性在数据注释包。他们正在检查属性内的isnull,但它应该是IsNullOrEmpty检查你的导入属性是否为空,如果空传递为空的问题应该解决。这只发生在CSV上的原因是因为在excel表格上它是空的而不是空的。

参考数据注释反编译代码。

namespace DataAnnotationsExtensions
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class EmailAttribute : DataTypeAttribute
{
private static Regex _regex = new Regex(@"^((([a-z]|'d|[!#'$%&''*'+'-'/='?'^_`{'|}~]|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])+('.([a-z]|'d|[!#'$%&''*'+'-'/='?'^_`{'|}~]|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])+)*)|(('x22)(((('x20|'x09)*('x0d'x0a))?('x20|'x09)+)?((['x01-'x08'x0b'x0c'x0e-'x1f'x7f]|'x21|['x23-'x5b]|['x5d-'x7e]|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])|(''(['x01-'x09'x0b'x0c'x0d-'x7f]|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF]))))*((('x20|'x09)*('x0d'x0a))?('x20|'x09)+)?('x22)))@((([a-z]|'d|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])|(([a-z]|'d|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])([a-z]|'d|-|'.|_|~|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])*([a-z]|'d|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])))'.)+(([a-z]|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])|(([a-z]|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])([a-z]|'d|-|'.|_|~|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])*([a-z]|['u00A0-'uD7FF'uF900-'uFDCF'uFDF0-'uFFEF])))'.?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public EmailAttribute()
: base(DataType.EmailAddress)
{
}
public override string FormatErrorMessage(string name)
{
if (ErrorMessage == null && ErrorMessageResourceName == null)
{
ErrorMessage = ValidatorResources.EmailAddressAttribute_Invalid;
}
return base.FormatErrorMessage(name);
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
string valueAsString = value as string;
return valueAsString != null && _regex.Match(valueAsString).Length > 0;
}
}
}

祝你有美好的一天