7

I have a "Thingy" controller for that looks like:

[HttpPost]
public ActionResult DeleteConfirmed(long? id) {
    // <Validate ID, delete associated records>
    return RedirectToAction("Index", "Thingy");
}

However, RedirectToAction keeps having its route values populated with the id from the parameters, whereas I want it to leave id as null so it redirects to www.mywebsite.com/Thingy instead of www.mywebsite.com/Thingy/1

In fact, I can visit www.mywebsite.com/Thingy directly and it works as expected.

I have tried:

RedirectToAction("Index", "Thingy")
RedirectToAction("Index", "Thingy", new { })
RedirectToAction("Index", "Thingy", new { id = (long?)null })

The last is particularly amusing because it redirects to www.mywebsite.com/Thingy?id=1 where as the others redirect to www.mywebsite.com/Thingy/1.

3
  • 1
    Try adding this before the redirect in your first example: RouteData.Values.Remove("id");. I have a feeling the route values you specify are being merged with the original route values. Commented Oct 22, 2014 at 21:47
  • Brilliant, works. Make it an answer and I'll accept since it effectively fixes my issue. Commented Oct 22, 2014 at 21:50
  • 1
    I'm happy it works, I was in the middle of testing it when you commented. Commented Oct 22, 2014 at 21:52

1 Answer 1

16

Add the following before your RedirectToAction() in your first example:

RouteData.Values.Remove("id");

I have a feeling the route values you specify are being merged with the original route values.

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

1 Comment

Wow. It was difficult to find. +1

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.