简体   繁体   English

asp.net mvc RedirectToAction(“Index”)vs Index()

[英]asp.net mvc RedirectToAction(“Index”) vs Index()

Say I have a controller with an Index Method and a Update Method. 假设我有一个带有索引方法和更新方法的控制器。 After the Update is done I want to redirect to Index(). 更新完成后,我想重定向到Index()。 Should I use return RedirectToAction("Index") or can I just call return Index()? 我应该使用return RedirectToAction(“Index”)还是只能调用return Index()? Is there a difference? 有区别吗?

public ActionResult Index()
{
  return View("Index", viewdata);
}

public ActionResult Update()
{
  // do updates
  return RedirectToAction("Index"); or
  return Index();
}

使用重定向,否则客户端上的URL将保持与发布的URL相同,而不是与Index操作对应的URL。

Other things to consider: 其他需要考虑的事项:

  • Redirect action after a POST will act more nicely when the user clicks Refresh button, since they won't be prompted to resend data to server. 当用户单击“刷新”按钮时,POST后的重定向操作将更好地运行,因为不会提示他们将数据重新发送到服务器。

  • Form data will be lost with the redirect action unless you maintain them explicitly through, say, TempData. 表单数据将通过重定向操作丢失,除非您通过TempData明确地维护它们。 Without doing this, your form controls won't have any value after a POST, which may be undesirable in some cases. 如果不这样做,表单控件在POST后将没有任何值,这在某些情况下可能是不合需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM