3

Using the standard route pattern

{controller}/{action}/{code}

and then a standard ActionLink in my view

<%: ActionLink("Details", "Details", new { code = item.Code }) %>

When the user has entered "N/A" as their code I see the following URL

http://localhost/AbsenceCode/Details/N/A

When I expect to see this

http://locahost/AbsenceCode/Details/N%2FA

Also, if the user has "A:B" as their code I see the correct URL (colon is url escaped) but I get a "400 bad request" from the server.

Does anyone have solutions to these problems? I cannot force the customer to use URL safe characters only.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Peter Morris
  • 20,174
  • 9
  • 81
  • 146

2 Answers2

0

Try

<%: Html.ActionLink("Details", "Details", new { code = Url.Encode(item.Code) }) &>
Jon
  • 16,212
  • 8
  • 50
  • 62
  • Forward slash is a valid url character so it won't get escaped. Also, it seems that any %xx escape characters are seen as bad requests by ASP MVC – Peter Morris Nov 12 '10 at 13:24
0

I've come up with a solution, source code is available here: https://mrpmorris.blogspot.com/2012/08/asp-mvc-encoding-route-values.html

Peter Morris
  • 20,174
  • 9
  • 81
  • 146