网址验证属性将“ localhost”标记为无效网址

编程入门 行业动态 更新时间:2024-10-28 08:22:24
本文介绍了网址验证属性将“ localhost”标记为无效网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我们的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 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进行验证: github/Microsoft/referencesource/blob/master/System.ComponentModel.DataAnnotations/DataAnnotations/UrlAttribute.cs# L46 。

UrlAttribute validates against the RegEx shown in the source code here: github/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 , or ftp://. See Line 33 of the same source file for that.

更多推荐

网址验证属性将“ localhost”标记为无效网址

本文发布于:2023-10-29 12:52:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网址   标记   属性   localhost

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!