向客户报告模型状态和应用程序错误的推荐方法是什么?

发布于 2024-10-20 06:16:49 字数 1270 浏览 1 评论 0原文

我想知道向浏览器报告将显示给用户的应用程序或模型状态错误的最佳实践是什么。你能抛出一个异常并在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/" });

I'm wondering what the best practice is in reporting back to the browser about application or model state errors that would be displayed to the user. Can you throw an exception and handle it in the error handler of the jquery post? For example, consider this method:

[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();           
        }

Is it correct to throw an exception in this case when the user is null? Or should I instead do this and handle it in the success handler of the 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/" });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2024-10-27 06:16:49

不要通过抛出异常来处理验证。如果您要发送 JSON 响应,请在 JSON 响应中包含客户端所需的所有内容:

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/" 
});

如果您要返回视图,请添加模型状态错误,表单上的 HTML 帮助程序将完成其余操作:

ModelState.AddModelError("email", "The email you entered does not exist in our system.  Please enter the email address you used to sign up.");
return View();

Don't handle validation by throwing exceptions. If you are sending a JSON response include all that's needed to the client in the JSON response:

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/" 
});

and if you are returning a view add a model state error and the HTML helpers on your form will do the rest:

ModelState.AddModelError("email", "The email you entered does not exist in our system.  Please enter the email address you used to sign up.");
return View();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文