使用DataType
Attribute并传入DataType.Phone
值和从Phone
继承并自动设置DataType
的DataType.Phone
Attribute有什么区别?
这两类之间有什么区别吗?
class Person {
[DataType(DataType.PhoneNumber)]
public string PhoneNumber {get;set;}
}
class Person {
[Phone]
public string PhoneNumber {get;set;}
}
参考方案
TLDR:[Phone]
提供验证逻辑,而[DataType]
不提供
继承链如下所示:Attribute
↳ValidationAttribute
↳DataTypeAttribute
↳PhoneAttribute
因此,这两个都是ValidationAttribute
的实例,但是都不提供开箱即用的验证。 DataType基类仅提供用于分配enum DataType
的结构,而将覆盖验证保留给调用者DataType
-根据文档:
将DataTypeAttribute
属性应用于数据字段时,必须执行以下操作:
发出适当的验证错误。
DataType
-根据源代码:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Parameter, AllowMultiple = false)]
public class DataTypeAttribute : ValidationAttribute
{
/// <summary> Override of <see cref="ValidationAttribute.IsValid(object)" /> </summary>
/// <remarks>This override always returns <c>true</c>. Subclasses should override this to provide the correct result.</remarks>
/// <param name="value">The value to validate</param>
/// <returns>Unconditionally returns <c>true</c></returns>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception>
public override bool IsValid(object value)
{
EnsureValidDataType();
return true;
}
}
另外:由于要求您重写IsValid
,所以我不确定.NET为什么没有将类和属性标记为abstract
来以编程方式保证实现。PhoneAttribute
-验证逻辑
因此,如果您确实想提供验证,并且使用的是.NET 4.5+或.NET Core,则可以使用[Phone]
属性,但是验证机制也随时间而改变,并且您可能会有所不同关于什么构成业务流程的有效输入的一组规则。
.NET Framework最初使用以下正则表达式:
const string pattern = @"^(\+\s?)?((?<!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$";
但这在.NET Framework 4.7.2中每个this change description中已弃用,这可能是由于Regular Expression Best Practices中不受限制的输入引起的注入/安全问题。
如果要继续使用正则表达式验证,则必须在configuration
文件的appsettings
> .config
部分中设置以下内容:
<add key="dataAnnotations:dataTypeAttribute:disableRegEx" value="false"/>
测试项目包括根据PhoneAttributeTests.cs应通过/失败哪些输入的示例,如果要针对(不建议使用的)正则表达式验证引擎测试匹配的输入,则为Regexr page。
这是一些用于各种.NET版本的源代码和文档的链接:
| .NET Core | .NET Core 2.1 | .NET 4.7.2 | .NET |
------------------|-------------------|----------------|-----------------|----------------|
DataTypeAttribute | github.com/dotnet | source.dot.net | referencesource | docs.microsoft |
PhoneAttribute | github.com/dotnet | source.dot.net | referencesource | docs.microsoft |
注意:当前[Phone]
的文档错误地指出验证使用正则表达式,自4.7.2+或.NET Core中的任何地方以来,这都是不正确的,因此我submitted this PR to update
我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……
.Net LINQ-使用其他字典过滤字典 - c#我有两个相同类型的字典,A和B。Dictionary<string, IEnumerable<object>> 我使用对象来表示具有属性“ Id”的复杂类型。我正在寻找A中具有B中存在的对象(使用Id)但在另一个键下的所有项目。基本上是要判断对象是否已移动键。 A是新字典,B是旧字典。有没有使用LINQ完成此操作的合理方法?我希望结果…
在ASP.NET MVC中创建数据库回调的最有效方法 - c#我有一个ASP.NET MVC网页,该网页基本上通过日期过滤器显示MS SQL数据库中表的行。当新行插入数据库表时,我想用新行列表更新网页视图。实现此目标的最有效方法是什么?基本上,我想从我的JavaScript创建一个到数据库服务器的回调,以用新结果更新UI。假设数据库表中的行数很大。(〜1百万)谢谢,cas 参考方案 如果数据库更新非常频繁,则可以按特定…
.NET XPathNavigator找不到XPath查询中指定的元素(但是XPath查询在XMLSpy中有效) - c#我有以下C#代码段,用于使用XPath在正在输入的XML文件中查找错误:string xml; // the XML is passed as a parameter as the string below using (Stream messageStream = new MemoryStream(xml)) { IXPathNavigable sourc…
如何使用JavaScript访问嵌入式ASP.NET GlobalResources? - javascript我正在开发一个遗留的ASP.NET项目,该项目正试图缓慢地进行调整,但是如果没有像巧克力手指屋一样塌陷的情况,我将无法进行重大更改。我试图为此找到解决方案,但由于术语的特定混合(“ javascript”,“ embedded”和/或“ resource”只是为我提供了有关如何嵌入.js文件的信息,而失败了)。 。),这可能是一种怪异的处理方式。该项目将Ap…