asp.net mvc RedirectToAction("Index")vs Index()

ter*_*tyl 16 c# asp.net asp.net-mvc

假设我有一个带有索引方法和更新方法的控制器.更新完成后,我想重定向到Index().我应该使用return RedirectToAction("Index")还是只能调用return Index()?有区别吗?

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

public ActionResult Update()
{
  // do updates
  return RedirectToAction("Index"); or
  return Index();
}
Run Code Online (Sandbox Code Playgroud)

tva*_*son 23

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


Buu*_*yen 11

其他需要考虑的事项:

  • 当用户单击"刷新"按钮时,POST后的重定向操作将更好地运行,因为不会提示他们将数据重新发送到服务器.

  • 表单数据将通过重定向操作丢失,除非您通过TempData明确地维护它们.如果不这样做,表单控件在POST后将没有任何值,这在某些情况下可能是不合需要的.