服务端验证用户提交信息时为了实现多语言使用了资源文件,如:

using System.ComponentModel.DataAnnotations;

public class UserModel
{ [Display(ResourceType =typeof(AppStrings),Name ="Field_User_Email")]
[Required(ErrorMessageResourceType = typeof(AppStrings), ErrorMessageResourceName = "Validation_Required")]
[EmailAddress(ErrorMessageResourceType = typeof(AppStrings), ErrorMessageResourceName = "Validation_Format")]
[StringLength(, ErrorMessageResourceType = typeof(AppStrings), ErrorMessageResourceName = "Validation_MaxLength")]
public string Email { get; set; } //...
}

测试时出现如下错误:

Cannot retrieve property 'Name' because localization failed.  Type 'Resources.AppStrings' is not public or does not contain a public static string property with the name 'Field_User_Email'.

解决方法:

设置资源文件的属性,将Custom Tool改为"PublicResXFileCodeGenerator"

Asp.net MVC 服务端验证多语言错误-LMLPHP

05-11 22:07