我想知道在向浏览器报告应用程序或模型状态错误方面的最佳实践是什么,这些错误将显示给用户。您可以抛出异常并在jquery post的错误处理程序中处理它吗?例如,考虑以下方法:
[HandlerErrorWithAjaxFilter, HttpPost]
public ActionResult RetrievePassword(string email)
{
User user = _userRepository.GetByEmail(email);
if (user == null)
throw new ClientException("The email you entered does not exist in our system. Please enter the email address you used to sign up.");
string randomString = SecurityHelper.GenerateRandomString();
user.Password = SecurityHelper.GetMD5Bytes(randomString);
_userRepository.Save();
EmailHelper.SendPasswordByEmail(randomString);
if (Request.IsAjaxRequest())
return Json(new JsonAuth { Success = true, Message = "Your password was reset successfully. We've emailed you your new password.", ReturnUrl = "/Home/" });
else
return View();
}
当用户为null时,抛出异常是否正确?还是我应该这样做,并在jquery post的成功处理程序中处理它:
return Json(new JsonAuth { Success = false, Message = "The email you entered does not exist in our system. Please enter the email address you used to sign up.", ReturnUrl = "/Home/" });