How to prevent users from visiting Deleted action method directly?
Scenario:
Clicking a delete hyperlink on one product of a product list will invoke /Product/Delete
HttpGet action method. The user then clicks the confirmation button to invoke /Product/Delete
HttpPost action method which in turn redirect the user to /Product/Deleted
HttpGet action method.
I 开发者_开发知识库want to prevent users from skipping /Product/Delete
and directly invoking /Product/Deleted
.
Before redirecting put something into TempData
. Then in the Deleted
action verify if this something is present in the 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();
}
You should be aware that there is a price to pay with this. If the user presses F5 while browsing the /product/deleted
action he will get 404. So basically what you are trying to do is bad design and I would recommend you avoiding it.
精彩评论