Update URL with new parameter value in controller action

I have an action that takes an id as a parameter and gets the corresponding item with the help of a web service. The web service's method that returns the item will either return the item with the requested id, or will create a new item (new version of the item) with a new id. So, in the case that a new item is created, the response's header should be updated with the new item's id.

Here's what the route corresponding to that controller action looks like:

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"default",
"MyArea/{Controller}/{action}/{id}",
new { controller = "MyController", action = "Index", id = UrlParameter.Optional }
);
}


So when an user requests the item with the id 12, the request's path will look like:

MyArea/MyController/MyAction/12

Let's say the web service creates a new version of the above item, and its id is 13, I'd like to update the URL accordingly. Here's what my action looks like:

[HttpGet]
{
try
{
Item myItem = await _myWS.Channel.GetItemAsync(id);

// If a new version has been created, change the url in order to show the new id
if (myItem.Id != id)
{
Response.StatusCode = 301;
string newUrl = Request.Path;
int lastSlashIndex = newUrl.LastIndexOf("/");
if (lastSlashIndex > 0)
newUrl = newUrl.Substring(0, lastSlashIndex + 1) + myItem.Id;

Response.End();
}

return View("MyView", (ItemViewModel)myItem);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}


I was wondering if there was a better way to reconstruct the URL with the updated id. I also tried reconstructing the URL with the Request.RequestContext.RouteData.Values, but I thought the code was less readable and more complicated, but I don't like the string replace neither, because I think it's less long term friendly.

Bonus

The reason I reconstructed the URL this way instead of a simple redirection it's because I thought the way I was doing avoided a second call to the action, but I was wrong. Is there a "hack" to avoid redirection and to just update the shown URL?

I believe you want RedirectToActionPermanent:
if (myItem.Id != id)

• Thank you for your answer. You are very correct about that. It would indeed be more readable, but I'd like to avoid my action to get called again, because that means the web service method GetItemAsync would get called twice needlessly. See, when a new item is created, that method returns the new item, so redirecting to the same action would call again the method to get the same item, so I'm trying to avoid that. I'll edit my post accordingly, because it lacks this slight precision. – actaram Mar 20 '17 at 15:41
• The only way I know of is the javascript history api, specifically the pushState function. – Jason P Mar 20 '17 at 15:54