Asp.net mvc 3 使用两个属性的MVC3 Custon验证属性
我正在尝试编写一个自定义验证类来验证电子邮件是否已被使用。为了做到这一点,我有一个方法来统计有问题的电子邮件地址的用户配置文件。很简单,但是如果用户正在更新他们的配置文件,我需要从我提到的计数中排除他们的配置文件。为此,我编写了以下方法:Asp.net mvc 3 使用两个属性的MVC3 Custon验证属性,asp.net-mvc-3,data-annotations,customvalidator,Asp.net Mvc 3,Data Annotations,Customvalidator,我正在尝试编写一个自定义验证类来验证电子邮件是否已被使用。为了做到这一点,我有一个方法来统计有问题的电子邮件地址的用户配置文件。很简单,但是如果用户正在更新他们的配置文件,我需要从我提到的计数中排除他们的配置文件。为此,我编写了以下方法: public static bool IsEmailUnique(string email, int userprofileId) { int count = -1; using (var db = new TRDataContext())
public static bool IsEmailUnique(string email, int userprofileId)
{
int count = -1;
using (var db = new TRDataContext())
{
count = (from u in db.Userprofiles
where u.email == email &&
(userprofileId == 0 || u.userprofile_id != userprofileId)
select u).Count();
}
return count == 0;
}
这在新用户的自定义验证器类中运行良好,因为我需要做的就是为userprofile_id传入一个0
public class EmailValidatorAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null)
return false;
return Userprofile.IsEmailUnique(value.ToString(), 0);
}
}
并像这样实施
[DisplayName("Email Address")]
[Required(ErrorMessage = "Required")]
[RegularExpression(EmailRegex, ErrorMessage = "Invalid email address")]
[EmailValidator(ErrorMessage = "Already in use")]
public string email { get; set; }
但是,我现在需要将当前用户的userprofile\u id传递给EmailValidator。我很难弄明白这一点
任何帮助都将不胜感激
谢谢 您可以从
HttpContext
获取:
string currentUsername = HttpContext.Current.User.Identity.Name;
在我因提议在方法中使用HttpContext.Current
而被判刑之前,您可以使用提供者:
public class EmailValidatorAttribute : ValidationAttribute
{
public Func<HttpContextBase> ContextProvider = () => new HttpContextWrapper(HttpContext.Current);
public override bool IsValid(object value)
{
if (value == null)
return false;
string currentUsername = ContextProvider().User.Identity.Name;
...
}
}
公共类EmailValidatorAttribute:ValidationAttribute
{
public Func ContextProvider=()=>新的HttpContextWrapper(HttpContext.Current);
公共覆盖布尔值有效(对象值)
{
如果(值==null)
返回false;
字符串currentUsername=ContextProvider().User.Identity.Name;
...
}
}
现在可以很容易地在单元测试中模拟它。您可以从
HttpContext
获取它:
string currentUsername = HttpContext.Current.User.Identity.Name;
在我因提议在方法中使用HttpContext.Current
而被判刑之前,您可以使用提供者:
public class EmailValidatorAttribute : ValidationAttribute
{
public Func<HttpContextBase> ContextProvider = () => new HttpContextWrapper(HttpContext.Current);
public override bool IsValid(object value)
{
if (value == null)
return false;
string currentUsername = ContextProvider().User.Identity.Name;
...
}
}
公共类EmailValidatorAttribute:ValidationAttribute
{
public Func ContextProvider=()=>新的HttpContextWrapper(HttpContext.Current);
公共覆盖布尔值有效(对象值)
{
如果(值==null)
返回false;
字符串currentUsername=ContextProvider().User.Identity.Name;
...
}
}
现在可以很容易地在单元测试中模仿它。我也遇到了类似的问题。首先,我在我的用户存储库中实现了一个“CurrentUser”方法,看起来有点像这样:
//Gets the current user
public U_USER CurrentUser()
{
return GetUser(HttpContext.Current.User.Identity.Name);
}
public U_USER GetUser(string u)
{
return (from us in db.U_USER
where us.UserName == u
select us).FirstOrDefault();
}
然后,您可以使用以下类似的方式从验证属性内部获取id:
public class EmailValidatorAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
using(UserRepository ur = new UserRepository())
{
if (value == null)
return false;
var user = ur.CurrentUser();
return Userprofile.IsEmailUnique(value.ToString(), user.userprofile_id);
}
}
}
我也有过类似的问题。首先,我在我的用户存储库中实现了一个“CurrentUser”方法,看起来有点像这样:
//Gets the current user
public U_USER CurrentUser()
{
return GetUser(HttpContext.Current.User.Identity.Name);
}
public U_USER GetUser(string u)
{
return (from us in db.U_USER
where us.UserName == u
select us).FirstOrDefault();
}
然后,您可以使用以下类似的方式从验证属性内部获取id:
public class EmailValidatorAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
using(UserRepository ur = new UserRepository())
{
if (value == null)
return false;
var user = ur.CurrentUser();
return Userprofile.IsEmailUnique(value.ToString(), user.userprofile_id);
}
}
}