MVC3 Razor URL.Action Parameter Value with a slash. giving error

Small question about MVC3 Razor, I have a string id like "a / b" when I try to call the details or remove the Controller method. Getting error as a system assuming "a / b" as 2 parameters, but I have to pass this in one string value parameter.

- Change

< a href="@Url.Action("Details", "Search", new {id = "a/b"})">Details </a>


My controller / method is like Search / Details (id string)

And I want to send id = 'a / b'. but .Net thinks these are 2 parameters in the url.

Please suggest.


source to share

2 answers

It looks like the forward slashes are not auto-encoded, and the reason is probably because even though they are encoded (% 2f), by the time they reach the routing engine, they have been decoded back into a forward slash. (Find Rob in this post by Phil Haack (former MVC team manager)).

However .NET MVC Routing w / Url Encoding Problems creates the same problem, and it seems that the only way to solve this problem is to insert the encoded forward slash in the query string. Something like that:

< a href="@Url.Action("Details", "Search")?id=@Url.Encode("a/b")">Details </a>


and then, dealing with it in your method, referring to:





You must use an escaped URL for this parameter to be a%2Fb



All Articles