邮政重定向获取ASP.NET MVC和验证REST风格的URL [英] Post Redirect Get in ASP.NET MVC And Validation With Restful URLs

查看:166
本文介绍了邮政重定向获取ASP.NET MVC和验证REST风格的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有编辑页面的动作一个宁静的URL。这是在控制器作为一个编辑方法,它接受GET请求,并接受POST请求一个编辑方法上实现

I have a restful URL for the action of editing a page. This is implemented on the controller as an Edit method, which accepts GET requests and an Edit method that accepts POST requests.

这意味着你可以参观编辑URL,它会显示一个形式GET或保存表单的POST。

This means you can visit the Edit URL and it will display a form for a GET or save a form for a POST.

[HttpGet]
public ActionResult Edit(int id) {
    ...
}

[HttpPost]
public ActionResult Edit(EditModel model) {
    ...
}

后重定向-获取(PRG)模式似乎pretty黑色和白色,因为它本质上重定向每一个岗位回到一个GET操作。但是,我需要确信,这是做了正确的事情。

The Post-Redirect-Get (PRG) pattern seems pretty black and white, because it essentially redirects every POST back to a GET action. However, I need to be convinced that this is the correct thing to do.

我的计划是,在POST操作,如果模型是有效的,我将使用后重定向-GET模式给用户发送到一个合理的位置(可能是指数或详细操作)。

My plan is that in the POST action, if the model is valid I will use the Post-Redirect-Get pattern to send the user to a reasonable location (probably the Index or Details action).

不过,如果有一个模型验证的问题,我还是想只显示该视图。我不希望将用户重定向,因为这意味着馅的模型和的ModelState到临时数据,并重定向到付诸行动 - 然后添加逻辑到GET行动来处理临时数据。我可以通过简单地显示视图避免这一切。

However, if there is a model validation problem, I still want to just display the view. I don't want to redirect the user, because it means stuffing the model and ModelState into temporary data and redirecting to the GET action - and then adding logic into the GET action to handle the temp data. I could avoid all of this by simply displaying the view.

是,如果用户presses F5将重新提交表单,他们将与重新提出警告psented $ P $,但随后在同一页(要求他们修复验证错误)。然而,这似乎不太可能,他们会按F5而且也没有双提交的表单危险只会再次失败的验证。

Yes, if the user presses F5 it will re-submit the form and they will be presented with the "resubmission warning", but then the same page (asking them to fix validation errors). However, it seems unlikely that they will hit F5 and there is also no danger of a double-submission as the form will simply fail the validation once again.

如果验证通过,用户会被重定向,他们将双重提交安全的。

If the validation passes, the user will be redirected and they will be safe from double submissions.

所以,我应该采取额外code和充填数据到临时数据,以便严格按照PRG模式,或​​者是当窗体是有效的,数据存储更明智的使用PRG模式?

So should I implement additional code and stuff data into temp data in order to strictly follow the PRG pattern, or is it more sensible to use the PRG pattern when the form is valid and data is being stored?

推荐答案

您应该只做重定向如果表单信息有效;在提交错误的情况下,返回从相同的编辑方法的看法。

You should only do the redirect if the form information is valid; in the case of submission errors, return the view from the same Edit method.

这样做,这样的与PRG兼容的,因为如果你的模型是无效的,你是不是允许任何更改到服务器上对象的状态进行。 PRG的主要目的是prevent多个帖子,可以改变在联合国predictable方法服务器对象(例如,业务对象,数据库表等)的状态;在验证的例子,但是,用户可以打,因为他们想重新提交多次,他们将永远被送回验证错误的初步意见 - 没有任何服务器上的变化。因此,你必须是正确的:如果你的模型在presentation层通过验证只能发出重定向

Doing it this way is compliant with PRG, because if your model is invalid, you are not allowing any changes to be made to the state of objects on the server. PRG is designed primarily to prevent multiple posts that can change the state of server objects (e.g., business objects, database tables, etc.) in unpredictable ways; in your validation example, however, the user can hit resubmit as many times as they want and they will always be sent back to the initial view with validation errors--nothing on the server changes. Therefore, you have it right: only issue the Redirect if your model passes validation in your presentation tier.

这篇关于邮政重定向获取ASP.NET MVC和验证REST风格的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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