Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC: Passing a string parameter to an action using RedirectToAction()

Tags:

I would like to know how to pass a string parameter using RedirectToAction().

Let's say I have this route:

routes.MapRoute(
  "MyRoute",
  "SomeController/SomeAction/{id}/{MyString}",
  new { controller = "SomeController", action = "SomeAction", id = 0, MyString = UrlParameter.Optional }
);

And in SomeController, I have an action doing a redirection as follows:

return RedirectToAction( "SomeAction", new { id = 23, MyString = someString } );

I tried this redirection with someString = "!@#$%?&* 1" and it always fails, no matter if I encode the string. I tried encoding it with HttpUtility.UrlEncode(someString), HttpUtility.UrlPathEncode(someString), and with Uri.EscapeUriString(someString) to no avail.

So I resorted to us TempData to pass someString, but still, I would be curious to know how to make the code above work, just to satisfy my curiosity.