【发布时间】:2010-12-21 18:16:42
【问题描述】:
场景:
单击产品列表的一个产品上的删除超链接将调用/Product/Delete HttpGet 操作方法。然后用户单击确认按钮调用/Product/DeleteHttpPost 操作方法,该操作方法又将用户重定向到/Product/DeletedHttpGet 操作方法。
我想防止用户跳过/Product/Delete 直接调用/Product/Deleted。
【问题讨论】:
场景:
单击产品列表的一个产品上的删除超链接将调用/Product/Delete HttpGet 操作方法。然后用户单击确认按钮调用/Product/DeleteHttpPost 操作方法,该操作方法又将用户重定向到/Product/DeletedHttpGet 操作方法。
我想防止用户跳过/Product/Delete 直接调用/Product/Deleted。
【问题讨论】:
在重定向之前将一些内容放入TempData。然后在Deleted 操作中验证TempData 中是否存在此内容。
[HttpPost]
public ActionResult Delete()
{
// TODO: Delete
TempData["deleted"] = true;
return RedirectToAction("deleted");
}
public ActionResult Deleted()
{
if(TempData["deleted"] == null)
{
throw new HttpException(404, "not found");
}
return View();
}
您应该知道,这样做是有代价的。如果用户在浏览/product/deleted 操作时按F5,他将得到404。所以基本上你正在尝试做的是糟糕的设计,我建议你避免它。
【讨论】:
Delete 操作上的 [HttpPost] 属性。