假设我有一个这样的动作:

[HttpPost]
public ActionResult(MyObject obj)
{
  //Do a SQL insert that gets an Id for obj

  //Do some long-running operation in the background - don't wait for it to finish

  //Return a report of the object
  return View(obj);
}


有没有一种方法可以在POST之后修改URL,以便在末尾显示?id=1234?执行GET时有一个等效的操作(就像用户共享了页面一样),我只想显示报告。

最佳答案

您应该使用RedirectResult并将用户重定向到新的URL。

如果这样做,您将无法将任何内容传递给视图。

一种常见的做法是将其存储在TempData变量中:

[HttpPost]
public ActionResult(MyObject obj)
{
  //Do a SQL insert that gets an Id for obj

  //Do some long-running operation in the background - don't wait for it to finish
  TempData["obj"] 0 obj;
  //Return a report of the object
  return new RedirectResult();
}


您无法以编程方式从服务器更改URL。
如果您不想使用重定向,则可以在页面加载后使用JavaScript进行更改

10-04 23:15
查看更多