如何在发布/重定向/获取模式中处理服务器端错误? [英] How are server side errors handled in Post/Redirect/Get pattern?

查看:144
本文介绍了如何在发布/重定向/获取模式中处理服务器端错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于成功的用例,发布/重定向/获取"(PRG)工作流程非常简单:只需将(客户端)重定向到所需页面即可.但是,如果在服务器端验证期间遇到错误,并且我们想在再次显示输入页面时保留输入,该怎么办?

For the successful use case, the Post/Redirect/Get (PRG) work flow is pretty simple: simply redirect (client-side) to the desired page. But what about cases when errors are encountered during server-side validation and we want to preserve the inputs when we display the input page again?

据我所知,有两种方法:一种是在错误期间(因此不考虑PRG模式)在POST提交表单(即没有重定向)之后简单地重新呈现输入页面;或者,重定向到输入页面,并将以前的输入存储在渲染过程中以后可以检索到的位置(例如会话).两者都有缺点:首先,我们面临PRG模式可以帮助我们避免的问题(例如,可书签性,重复提交);第二种方法导致GET不一致(第一个GET将找到存储的输入,后续的GET可能找不到).除了这里提到的以外,还有其他选择吗?我希望社区能够提供有关如何最好地处理此案的意见.

As far as I can tell, there are two approaches: simply re-render the input page after the form POST submission (i.e. no redirection) during errors (thus disregarding the PRG pattern); or, redirect to the input page, and store the previous inputs somewhere it can be retrieved later (e.g. session), during rendering. Both have drawbacks: in the first, we are presented with the problems PRG pattern helps us to avoid (e.g. bookmarkability, double submission); the second approach leads to inconsistent GETs (first GET will find the stored inputs, subsequent GETs might not). Are there other alternatives to those mentioned here? I am hoping for inputs from the community on how this case is best handled.

推荐答案

通常,只有在成功提交后,我才会以您描述重定向的第一种方式执行此操作.很难看到将包含无效数据的表单添加为书签的真实用例.另一方面,在确认页面上添加书签(成功提交后)通常很有意义.

I typically do it the first way you describe—redirect only in the event of a successful submission. It's hard to see a real use case for bookmarking a form containing invalid data; on the other hand it often makes sense to bookmark a confirmation page (after successful submit).

这篇关于如何在发布/重定向/获取模式中处理服务器端错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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