网址验证属性将“ localhost”标记为无效网址 [英] Url validation attribute marks `localhost` as invalid Url
问题描述
在我们的ASP.MVC项目中,我们使用DataAnnotations属性进行验证。字段之一应包含Url,并用 [Url]
属性标记。但是,如果我将 http:// localhost:13030
放入字段值,则不会通过验证。
In our ASP.MVC project we are using DataAnnotations attributes for validation. One of the fields should contains Url and it is marked with [Url]
attribute. But if I put http://localhost:13030
into the field value it isn't passing the validation.
是否可以使用该属性将 localhost
定义为有效目标?
Is there any way to use the attribute to define localhost
as a valid target?
推荐答案
UrlAttribute
针对此处源代码中显示的RegEx进行验证: https://github.com/Microsoft/referencesource/blob/master/System.ComponentModel.DataAnnotations/DataAnnotations/UrlAttribute.cs# L46 。
UrlAttribute
validates against the RegEx shown in the source code here: https://github.com/Microsoft/referencesource/blob/master/System.ComponentModel.DataAnnotations/DataAnnotations/UrlAttribute.cs#L46.
http:// localhost
由于缺少。
。
其他选项请参见以下答案:如何在C#中验证localhost的网址。
See this answer for other options: How I can validate urls in C# for localhost.
编辑:根据源代码,您可以将 dataAnnotations:dataTypeAttribute:disableRegEx
添加到您的AppSettings中,并将其值设置为 true
。这将导致 UrlAttribute
验证过程仅 检查其是否以 http://
, https://
或 ftp://
。请参阅第33行相同的源文件。
According to the source code, you could add dataAnnotations:dataTypeAttribute:disableRegEx
to your AppSettings and set its value to true
. This would cause the UrlAttribute
validation process to only check that it begins with http://
, https://
or ftp://
. See Line 33 of the same source file for that.
这篇关于网址验证属性将“ localhost”标记为无效网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!