Fra*_*ger 1 asp.net-mvc usability
ASP.NET MVC模式通过post提交表单,然后重定向到相同或不同的URL非常容易编码.
想象一下这种情况:
我担心即使我为POST和GET使用两个不同的URL(例如/ products/42/edit和/ products/42),浏览器仍然会要求重新发布并可以销毁数据.我错了吗?
可以使用哪些替代方法,以便在提交产品更改后,用户可以安全地点击刷新以获取更新的视图?
更新我现在看到我的问题和我的设计混乱,我为此道歉.我发现在POST和GET之间共享URL(操作)对我来说是一个坏主意.我是否正确地假设,如果这两者不同,那么我将不会有"刷新导致rePOST"问题?
虽然我同意在帖子完成后没有将用户再次返回编辑视图的主要答案中所说的内容; 它没有回答为什么推送刷新重新发布表单的问题.
以下是"在浏览器中刷新导致另一个帖子"问题的解决方案.
目前你这样做:
[AcceptVerbs(HttpVerbs.Post)] // <-- this action will be used for POSTs
EditProduct(string data1, string data2)
{
// Handle Data, save to DB
// Do some work
return View("EditProduct"); // <-- You are rendering the view from
} // A post action - this is bad!
Run Code Online (Sandbox Code Playgroud)
你什么时候应该这样做:
[AcceptVerbs(HttpVerbs.Post)] // <-- This action will be used for POSTs
EditProduct(string data1, string data2)
{
// Handle Data, save to DB
// Do some work
return RedirectToAction("EditProduct"); // <-- Redirect to a GET Action
}
[AcceptVerbs(HttpVerbs.Get)] // <-- This action will be used for GETs
EditProduct()
{
return View("EditProduct"); // <-- Render the view from the GET action
} // So when you refresh it will refresh the GET
Run Code Online (Sandbox Code Playgroud)
关键点是,不要返回View以响应POST,否则浏览器中的最后一个请求是POST请求,并且浏览器中的推送刷新将重新发布.相反,当您完成后期操作后,使用RedirectToAction()重定向到"GET"操作.GET操作又返回View.这意味着浏览器中的先前请求是GET请求,如果您按下刷新它将再次获取它,而不是重新发布它.当我开始使用MVC时,我犯了同样的错误.
你的更新:是的.
使用/ product/{id} /进行查看,使用/ product/{id}/edit进行编辑,然后在编辑后将其重定向到/ product/{id} /.
问题解决了.想知道你为什么/正在使用/ product/{id}/edit进行查看和编辑.
归档时间: |
|
查看次数: |
4296 次 |
最近记录: |