C# 两个属性服务于同一个函数,只使用一个远程验证方法

C# 两个属性服务于同一个函数,只使用一个远程验证方法,c#,asp.net,C#,Asp.net,在提供相同功能的两个属性中,是否可以仅使用一种方法进行远程验证?例如,我有两个用于存储电子邮件的属性:第一个属性只获取没有扩展名的电子邮件地址(例如,[name]@email.com);第二个是获取整个电子邮件地址(例如[name@email.com]) 型号: [MaxLength(50), Display(Name = "Email Address.")] [Remote("CheckExistingEmail", "Account", AdditionalFields = "EmailEx

在提供相同功能的两个属性中,是否可以仅使用一种方法进行远程验证?例如,我有两个用于存储电子邮件的属性:第一个属性只获取没有扩展名的电子邮件地址(例如,[name]@email.com);第二个是获取整个电子邮件地址(例如[name@email.com])

型号:

[MaxLength(50), Display(Name = "Email Address.")]
[Remote("CheckExistingEmail", "Account", AdditionalFields = "EmailExtension", ErrorMessage = "Email already exists")]
public string EmailWithoutExtension { get; set; }

[Email, MaxLength(100), Display(Name = "Email Address")]
[Remote("CheckExistingEmail", "Account", ErrorMessage = "Email already exists.")]
public string EmailWithExtension { get; set; }

[HiddenInput]
public string EmailExtension { get; set; }
[AjaxOnly, AllowAnonymous]
public ActionResult CheckExistingEmail(string EmailWithExtension)
{
    if(string.IsNullOrEmpty(EmailWithExtension))
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    var userEmail = AccountManager.FindByEmail(EmailWithExtension);
    if (userEmail == null)
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    return Json(false, JsonRequestBehavior.AllowGet);
}
控制器:

[MaxLength(50), Display(Name = "Email Address.")]
[Remote("CheckExistingEmail", "Account", AdditionalFields = "EmailExtension", ErrorMessage = "Email already exists")]
public string EmailWithoutExtension { get; set; }

[Email, MaxLength(100), Display(Name = "Email Address")]
[Remote("CheckExistingEmail", "Account", ErrorMessage = "Email already exists.")]
public string EmailWithExtension { get; set; }

[HiddenInput]
public string EmailExtension { get; set; }
[AjaxOnly, AllowAnonymous]
public ActionResult CheckExistingEmail(string EmailWithExtension)
{
    if(string.IsNullOrEmpty(EmailWithExtension))
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    var userEmail = AccountManager.FindByEmail(EmailWithExtension);
    if (userEmail == null)
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    return Json(false, JsonRequestBehavior.AllowGet);
}

我对这两个属性尝试了方法重载,但没有成功,因为这会混淆远程验证。我也在考虑将属性绑定到一个变量中,但不确定该怎么做。关于如何在不创建服务于相同功能的不同方法名称的情况下实现这一点,有什么建议吗?

我找到了方法。所以我只需要添加所有参数,检查哪个参数有值,然后验证值

[AjaxOnly, AllowAnonymous]
public ActionResult CheckExistingEmail(string emailWithoutExtension, string emailWithExtension, string emailExtension)
{
    if(string.IsNullOrEmpty(emailWithoutExtension) && string.IsNullOrEmpty(EmailWithExtension))
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    var email = string.Emtpy;
    if(!string.IsNullOrEmpty(emailWithoutExtension)) && !string.IsNullOrEmpty(emailExtension))
    {
        email = emailWithoutExtension + emailExtension;
    }

    if(!string.IsNullOrEmpty(emailWithExtension) && string.IsNullOrEmpty(emailWithoutExtension))
    {
        email = emailWithExtension;
    }

    var account = AccountManager.FindByEmail(email);
    if (account == null)
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }

    return Json("Email already exists try another.", JsonRequestBehavior.AllowGet);
}
通过执行上述代码,两个属性将只使用一种方法