在ASP.NET MVC中自我引用MetadataType是否安全?

时间:2011-06-15 13:43:58

标签: asp.net-mvc-3 metadata

我已经看到了MVC的[MetadataType(T)]属性,它一切都很好,但是我想知道是否有任何自我引用它所放置的类的突出影响。我已经习惯了,尝试过它,并且效果非常好。事实上太好了。所以,我基本上想知道的是......我是否正在使用以下代码做一些危险的事情?

[MetadataType(typeof(RegisterViewModel))]
public class RegisterViewModel : IMember {
    [Required]
    [DataType(DataType.EmailAddress)]
    [RegularExpression(Text.RegularExpressions.Email, ErrorMessage = Text.ErrorMessages.Email)]
    [Display(Name = "Email Address")]
    [Rules("The name you'll login with. You can't use: <em>[ ] | ; , $ \\ < > \"</em>")]
    public string Email { get; set; }

    [Required(ErrorMessage = "You must enter a password.")]
    [StringLength(32, MinimumLength = 6)]
    [DataType(DataType.Password)]
    [RegularExpression(Text.RegularExpressions.Password, ErrorMessage = Text.ErrorMessages.Password)]
    [Display(Name = "Enter your password")]
    [Rules("Passwords must be between 6 and 32 characters, may contain any alphanumeric character and the symbols <em>@ # $ %</em> only.")]
    public string Password { get; set; }

    [Required(ErrorMessage = "You must confirm your password.")]
    [StringLength(32, MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Re-enter your password")]
    [RegularExpression(Text.RegularExpressions.Password, ErrorMessage = Text.ErrorMessages.Password)]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

1 个答案:

答案 0 :(得分:2)

没有意义。

[MetadataType]属性告诉MVC从代理类的属性读取属性,而不是类本身的原始属性。
它适用于无法向原始类添加属性的情况(例如,如果类是由设计者自动生成的)

如果没有此属性,MVC将从您的类本身读取属性。