3

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.

0

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I used the javascript option you mention, I did not want to run a new action yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.