jonathanpeppers
jonathanpeppers

Reputation: 26495

ASP.Net MVC 3 - Alter URL mid POST

Let's say I have an action like this:

[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);
}

Is there a way to modify the URL after the POST so it will display ?id=1234 at the end? There is an equivalent action for doing a GET (as if the user shared the page), and I'd like to just display the report.

Upvotes: 3

Views: 154

Answers (1)

Kenneth
Kenneth

Reputation: 28737

You should use a RedirectResult and redirect the user to the new URL.

If you do that you can't pass anything to the view though.

A common practice is to store it in the TempData variable:

[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();
}

You can't programmatically change the URL from the server. If you don't want to use a redirect, you could change it with JavaScript once the page has loaded

Upvotes: 2

Related Questions