【发布时间】:2016-12-22 09:18:36
【问题描述】:
【问题讨论】:
-
您可以从模型类更改此消息
-
您应该能够使用 DataAnnotation 中的属性
ErrorMessage = "..."更改 AccountViewModel.cs 中的这些错误消息。
标签: asp.net-mvc asp.net-core asp.net-identity
【问题讨论】:
ErrorMessage = "..." 更改 AccountViewModel.cs 中的这些错误消息。
标签: asp.net-mvc asp.net-core asp.net-identity
您应该重写IdentityErrorDescriber 的方法来更改身份错误消息。
public class YourIdentityErrorDescriber : IdentityErrorDescriber
{
public override IdentityError PasswordRequiresUpper()
{
return new IdentityError
{
Code = nameof(PasswordRequiresUpper),
Description = "<your error message>"
};
}
//... other methods
}
在Startup.cs 中设置IdentityErrorDescriber
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddErrorDescriber<YourIdentityErrorDescriber>();
}
【讨论】:
您可以在 RegisterViewModel 类中使用 DataAnnotations。事实上,如果您使用身份验证构建应用程序,您将得到如下结果:
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
显然,您可以将ErrorMessage 更改为您想要的任何内容!
【讨论】: