ASP.NET MVC 2 客户端验证触发不正确

发布于 2024-10-05 20:05:31 字数 1246 浏览 6 评论 0原文

我正在使用 DataAnnotations 在 ASP.NET MVC 2 项目中启用客户端验证。我遇到一个问题,我的 URL 验证正则表达式通过了单元测试,但在实际网站中失败了。

模型

[RegularExpression(UrlValidation.Regex, ErrorMessage = UrlValidation.Message)]
public string Url { get; set; }

Regex = "(([\w]+:)?//)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?"

消息 =“无效 Url”

使用 URL查看

<div class="editor-label">
    <%: Html.LabelFor(model => model.Url) %>
</div>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Url) %>
    <%: Html.ValidationMessageFor(model => model.Url) %>
</div>

结果 = http://www.chicagoshakes.com/main.taf?p=7,8

alt text

通过单元测试

[Test]
public void GetVarUrlPasses()
{
    var url = "http://www.chicagoshakes.com/main.taf?p=7,8";
    var regex = new Regex(UrlValidation.Regex);
    Assert.IsTrue(regex.IsMatch(url));
}

有谁知道为什么这通过了单元测试,但当我在浏览器中测试视图时却失败了?

I am using DataAnnotations to enable client-side validation in an ASP.NET MVC 2 project. I am having an issue where my URL validation regex passes my unit test, but it fails in the actual website.

Model

[RegularExpression(UrlValidation.Regex, ErrorMessage = UrlValidation.Message)]
public string Url { get; set; }

Regex = "(([\w]+:)?//)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?"

Message = "Invalid Url"

View

<div class="editor-label">
    <%: Html.LabelFor(model => model.Url) %>
</div>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Url) %>
    <%: Html.ValidationMessageFor(model => model.Url) %>
</div>

Result With URL = http://www.chicagoshakes.com/main.taf?p=7,8

alt text

Passing Unit Test

[Test]
public void GetVarUrlPasses()
{
    var url = "http://www.chicagoshakes.com/main.taf?p=7,8";
    var regex = new Regex(UrlValidation.Regex);
    Assert.IsTrue(regex.IsMatch(url));
}

Does anyone have any idea why this is passing the unit test, but failing when I test the view in a browser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-10-12 20:05:31

我可以看到该正则表达式有几个问题,但让您困惑的可能是 p=7,8 中的逗号。您的正则表达式与它不匹配,但 IsMatch 不要求它匹配;它非常高兴停在 7 处。我猜想客户端验证器隐式锚定了匹配。无论如何,正则表达式应该被锚定;在开头添加 ^ 并在末尾添加 $,您至少应该得到一致的结果。然后您可以更改正则表达式以适应逗号。

正则表达式中还有一个拼写错误:[a-fA-f\d] 中的第二个 f 应该是 F。而且,尽管将 \d\w 放在同一组方括号中并不是错误,但它是多余的; \w 匹配数字和字母,因此您可以删除 \d (并且 [\w\d] 可以简化为 <代码>\w)。最后,{2,2} 应该简单地为{2}

I can see several problems with that regex, but what's tripping you up is probably the comma in p=7,8. Your regex doesn't match it, but IsMatch doesn't require it to; it's perfectly happy stopping at the 7. I would guess that the client-side validator is implicitly anchoring the match. The regex should be anchored anyway; add a ^ to the beginning and $ to the end, and you should at least get consistent results. Then you can change the regex to accommodate the comma.

There's also a typo in the regex: the second f in [a-fA-f\d] should be F. And, although it's not an error to have \d and \w in the same set of square brackets, it is redundant; \w matches digits as well as letters, so you can remove the \d (and [\w\d] can be reduced to \w). Finally, {2,2} should be simply {2}.

萌无敌 2024-10-12 20:05:31

因为它正在通过单元测试,所以问题可能出在您的控制器或模型中。 Url 是否曾被直接访问过?

Because it's passing the unit test, the problem may be in your controller or model. Is Url ever directly accessed?

淡淡離愁欲言轉身 2024-10-12 20:05:31

艾伦摩尔基本上是对的,正则表达式是垃圾。我最终使用了这个正则表达式

另外,这是编写单元测试的正确方法:

[Test]
public void GetVarUrlPasses()
{
    var url = "http://www.chicagoshakes.com/main.taf?p=7,8";
    var attribute = new RegularExpressionAttribute(regex);
    Assert.IsTrue(regex.IsValid(url));
}

Alan Moore was basically right, the regex was junk. I ended up using a this regex.

Also, here's the proper way to write the unit test:

[Test]
public void GetVarUrlPasses()
{
    var url = "http://www.chicagoshakes.com/main.taf?p=7,8";
    var attribute = new RegularExpressionAttribute(regex);
    Assert.IsTrue(regex.IsValid(url));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文