什么是推荐的方式向客户汇报模型的状态和应用程序错误? [英] What's the recommended way to report model state and application errors to client?

查看:172
本文介绍了什么是推荐的方式向客户汇报模型的状态和应用程序错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道最好的做法是在报告给有关应用程序或模型状态错误的浏览器将显示给用户的内容。你可以抛出一个异常,它处理的jQuery的岗位错误处理程序?例如,考虑这个方法:

  [HandlerErrorWithAjaxFilter,HttpPost]
        公众的ActionResult RetrievePassword(字符串email)
        {
            用户USER = _userRepository.GetByEmail(电子邮件);

            如果(用户== NULL)
                抛出新ClientException(您输入在我们的系统中不存在的电子邮件请输入您用来注册的电子邮件地址。);

            字符串randomString = SecurityHelper.GenerateRandomString();
            user.password的= SecurityHelper.GetMD5Bytes(randomString);
            _userRepository.Save();

            EmailHelper.SendPasswordByEmail(randomString);

            如果(Request.IsAjaxRequest())
                返回JSON(新JsonAuth {成功= TRUE,消息=您的密码已成功重置我们已经通过电子邮件发送您的新密码。,RETURNURL =/首页/});
            其他
                返回查看();
        }
 

是否正确,当用户为空在这种情况下,引发异常?或者我应该做的,而不是这一点,它在处理的jQuery的岗位成功处理程序:

 返回JSON(新JsonAuth {成功=假,消息=您输入在我们的系统中不存在的邮箱,请输入您用来注册的电子邮件地址,RETURNURL =/首页/});
 

解决方案

不要通过抛出异常处理验证。如果要发送一个JSON响应,包括所有需要在JSON响应客户端:

 返回JSON(新JsonAuth {
    成功=假,
    消息=您输入在我们的系统中不存在的邮箱,请输入您用来注册的电子邮件地址,
    RETURNURL =/首页/
});
 

如果你正在返回一个视图中添加模型状态错误,您的窗体上的HTML助手将完成剩下的:

  ModelState.AddModelError(电子邮件,您输入的电子邮件并不在我们的系统中存在请输入您用来注册的电子邮件地址。);
返回查看();
 

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

解决方案

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

这篇关于什么是推荐的方式向客户汇报模型的状态和应用程序错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆