Reputation: 7173
I'll show what I'm trying to do with a simple example.
I have the routes
routes.MapRoute("",
"Message",
new { controller = "Home", action = "Index", myEnum = MyEnum.Message});
routes.MapRoute("",
"",
new { controller = "Home", action = "Index" });
And the action
public ActionResult Index(/*other values*/, MyEnum? myEnum = null)
{
// do some logic
var redirectToHomeUrl = userLoggedIn && myEnum.HasValue && myEnum.Value == MyEnum.Message;
if (redirectToHomeUrl)
{
// do some logic
return RedirectToAction("Index"); <-- problem here
}
// Other logic
}
Basically the user gets sent to mysite.com/Message and is logged out and shown a model dialogue message over the home page. When they act on this message and log back in, the page reloads. I detect that they still have the url for the message and want to redirect them back to the home url "mysite.com/".
However return RedirectToAction("Index")
still retains myEnum = MyEnum.Message
.
RouteData is persisted after the redirect so I get an infinite loop.
I've tried RedirectToRoute(null, new { action = "Index", controller = "Home"});
I've tried RedirectToAction("Index", new {})
I've tried RouteData.Values.Clear()
I've tried RouteData.Values["myEnum"] = null;
If your wondering RedirectToAction("Index", new { myEnum = null })
doesn't compile :)
Any idea's on how to redirect to the same Action but removing existing route values?
Cheers in advance.
Upvotes: 3
Views: 3298
Reputation: 67380
RedirectToAction("Index", new { myEnum = null })
might not compile, but this does:
RedirectToAction("Index", new { myEnum = (MyEnum?)null })
if you're wondering :)
I don't get the point of redirecting to the same action though. If all you want is to remove a value, set it to null
manually (or ignore it). If you actually want to call a different function (but the same action) (doesn't seem to be the case, but who knows), just return that function's result from yours directly.
Upvotes: 4