c# - Url 验证属性将 `localhost` 标记为无效 Url

标签 c# asp.net-mvc validation data-annotations

在我们的 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# - Url 验证属性将 `localhost` 标记为无效 Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45707293/

相关文章:

jquery - mvc 中对 api 操作的 ajax 请求不会执行成功事件 (asp.net webapi)

asp.net-mvc - 带有 Postgres 的 ASP.NET MVC; ORM 推荐?

javascript - 如何验证动态生成的包含 jquery 中的引导选项卡的表单

c# - SystemEvents.TimeChanged 触发两次

c# - asp.net 网站的大型下拉菜单

c# - 接受 pdf 页面放入 C# windows 窗体应用程序

javascript - 制作带有链接的可点击 <div>,如何只触发一个 Action ?

c# - C#设置加载和保存

javascript - Angular 2 : Custom validation for fields with numbers only

python - Django 请求在干净的表单中不可用