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.
I think the issue might be either in your route order, or in your controller. Here is some code that I got to work.
Route Definitions
Controller Action Methods
When I browse to
/Home/MoreTesting
I get the desired output of "23Hello" to output in my browser. Can you post your routes and your Controller code?OK, I know this question is a few days old but I wasn't sure if you got this issue sorted or not so I had a look. I played around with this for a while now and this is what the problem is and how you could solve it.
The problem you are having is that the special characters causing issues are one of the many (I think 20) special characters, such as % and ".
In your example the problem is the % character. As pointed out by
Priyank
here:The Url string (not query string parameter) can't handle %(%25), "(%22) and so on. Further, as pointed out by
Lee Gunn
in the same post: http://localhost:1423/Home/Testing/23/!%40%23%24%25%3f%26*%201 - (this will blow up)One of the ways to fix this is to remove
{MyString}
from the route mapping. To make your root mapping look like this:This will cause the post to generate this:
http://localhost:1423/Home/Testing/23?MyString=!%2540%2523%2524%2525%2B1
Now when you set
MyString
it will be turned into a query string parameter which works perfectly fine. I did try that and it did work.Priyank
also mentioned in the SO post I linked above that you maybe could solve this with a customValueProvider
but you would have to follow his linked article their to check if that is something that would apply to you.