I have a route that looks like this:
routes.MapRoute( "BlogTags", "Blog/Tags/{tag}", new { controller = "Blog", action = "BrowseTag", viewRss = false } );
And I am creating a URL using this route as follows:
<%= Html.RouteLink(Html.Encode(sortedTags[i].Tag), new { action = "BrowseTag", tag = sortedTags[i].Tag })%>
However, when using a tag with the # character (for example, "C #"), the routing mechanism does not hide it, so I get a URL that looks like this:
<a href="/Blog/Tags/C#">C
I need # to be escaped so that it looks like this:
<a href="/Blog/Tags/C%23">C
I tried to make Url.Encode in the tag before it entered the route, for example:
<%= Html.RouteLink(Html.Encode(sortedTags[i].Tag), new { action = "BrowseTag", tag = Url.Encode(sortedTags[i].Tag) })%>
But this makes the routing mechanism double escape # (which causes ASP.NET to crash with an invalid request error):
<a href="/Blog/Tags/C%2523">C
How can I make the routing mechanism escape this # character for me correctly?
Thank you for your help.
c # asp.net-mvc url-routing
Daniel Chambers
source share