在我们的ASP.MVC项目中,我们使用DataAnnotations属性进行验证。字段之一应包含Url,并用[Url]
属性标记。但是,如果我将http://localhost:13030
放入字段值中,则不会通过验证。
有什么方法可以使用该属性将localhost
定义为有效目标?
最佳答案
UrlAttribute
针对此处源代码中显示的RegEx进行验证:https://github.com/Microsoft/referencesource/blob/master/System.ComponentModel.DataAnnotations/DataAnnotations/UrlAttribute.cs#L46。http://localhost
不匹配,因为缺少.
。
有关其他选项,请参见此答案:How I can validate urls in C# for localhost。
编辑:根据源代码,您可以将dataAnnotations:dataTypeAttribute:disableRegEx
添加到您的AppSettings中,并将其值设置为true
。这将导致UrlAttribute
验证过程仅进行检查,以使其以http://
,https://
或ftp://
开头。为此,请参见同一源文件的Line 33。
关于c# - 网址验证属性将 `localhost`标记为无效网址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45707293/