2016-08-16 99 views
7

使用帶有ASP.NET標識的MVC Core我想更改從Register動作到達的ValidationSummary的默認錯誤消息。 任何意見將不勝感激。如何更改MVC Core ValidationSummary的默認錯誤消息?

ASP.NET Core Register Action

+0

您可以更改從模型類此消息 – Vipul

+0

你應該能夠改變這些錯誤消息在AccountViewModel.cs與DataAnnotation中的屬性'ErrorMessage =「...」'。 –

回答

7

您應該覆蓋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>(); 
} 

答案是從https://stackoverflow.com/a/38199890/5426333

-1

您可以在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你希望它是什麼!

+0

嗨Felix更改DataAnnotations不會影響驗證摘要。 – AG70

相關問題