【问题标题】:ASP.Net MVC 3 - Alter URL mid POSTASP.Net MVC 3 - 在 POST 中更改 URL
【发布时间】:2013-05-08 21:14:09
【问题描述】:

假设我有这样的动作:

[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 有一个等效的操作(就像用户共享页面一样),我只想显示报告。

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    您应该使用RedirectResult 并将用户重定向到新网址。

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

    一种常见的做法是将其存储在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 更改它

    【讨论】:

    • 我使用了你提到的 javascript 选项,我还不想运行新的操作。
    猜你喜欢
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多