EmailAttribute在ASP.NET Core的ViewModel中无法正确验证 [英] EmailAttribute is not validating correctly in a ViewModel from ASP.NET Core

查看:145
本文介绍了EmailAttribute在ASP.NET Core的ViewModel中无法正确验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的视图模型中的一个字段:

This is a field located in my viewmodel:

[Required(ErrorMessage = "Email is missing."), EmailAddress(ErrorMessage = "Email is not valid.")]
public string Email { get; set; }

(EmailAddress来自EmailAddressAttribute.EmailAddressAttribute()类型)
这是HTML中的相关部分:

(EmailAddress is from the EmailAddressAttribute.EmailAddressAttribute() type)
This is the relevant part from the HTML:

<div>
    <label for="inputEmail">EMAIL</label>
    <input id="inputEmail" ng-model="email" asp-for="Email" class="form-control" />
</div>
<div>
    <span asp-validation-for="Email" class="text-danger"></span>
</div>

当我在电子邮件文本框中键入myemail时,它将显示

When I type myemail in the email text box, it will say

电子邮件无效

Email is invalid

但是,当键入myemail@email时,视图模型(仅在前端)会将其视为正确.

However, when typing myemail@email, it will be viewed as correct by the view-model (only on the front-end).

public async Task<JsonResult> Register([FromForm]VisitViewModel vm)

这样正确的投放会将ModelState设置为无效并拒绝电子邮件,因此该部分还可以.

Casting like this properly puts the ModelState on invalid and rejects the email, so that part is okay.

尽管根据此答案,前端应将其指示为无效

According to this answer though, the frontend should indicate this as invalid

有人可以解释这里发生了什么吗?我绝对不知所措.

Can anybody explain what is happening here? I'm at an absolute loss.

推荐答案

客户端和服务器之间验证电子邮件的方式不一致.

There are inconsistencies in the way emails are validated between client and server.

jQuery验证使用HTML中定义的正则表达式标准.根据该表达式,email@email有效电子邮件地址.

jQuery Validation uses a regular expression defined in the HTML standard. According to that expression, email@email is a valid email address.

.Net EmailAddressAttribute具有其自己的正则表达式,您可以

The .Net EmailAddressAttribute has it's own regular expression, which you can find here. According that that expression, email@email is an invalid email address.

但是它变得更加令人困惑!如果您要创建.Net Core应用,则跨平台定位使用属性

But it gets even more confusing! If you're creating a .Net Core app, targeting cross platform uses the CoreFX version of the attribute which doesn't appear to use a regular expression at all. According to that method, email@email is a valid email address.

您可以通过在核心控制台应用程序的project.json中切换值来自己查看:

You see it for yourself by switching around values in the project.json of a Core Console App:

public static void Main(string[] args)
{
    var attr = new EmailAddressAttribute();
    var email = "email@email";
    var isValid = attr.IsValid(email); // false with "net452", true with "netcoreapp1.0"
}

你能做什么

我能想到的最快的解决方法是更改​​

What you can do

The quickest fix that I can think of would be to change the regular expression in the jQuery Validate email method to match the one from .Net, or create your own validation method to add to it which checks for this particular case.

您还可以回退从服务器返回的错误,这将向他们显示ModelState的任何错误的验证消息.这不是一个实时错误,但是他们仍然会收到一条很好的消息来处理不正确的电子邮件.

You can also just fallback on returning errors from the server which would show them a validation message for whatever is wrong with the ModelState. It wouldn't be a real-time error but they would still get a nice message to deal with the improper email.

这篇关于EmailAttribute在ASP.NET Core的ViewModel中无法正确验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆