tags:

views:

306

answers:

4

I want to generate a url from a tag name.

In my view (asp.net mvc) I have the following:

<%= Html.ActionLink(Html.Encode(tagName), 
"tagged" //action, 
"posts" //controller, 
new {tagName=Html.UrlEncode(tagName)} //querystring argument, 
new {@class="tag"} //html attributes) 
%>

But this generates the following URL if the tagName is "c#" (without the quotes):

http://localhost/posts/tagged/c%2523

which results in Http 400 error - Bad Request

On Stackoverflow, the url for the same "c#" tag generates "c%23" instead of "c%2523"

I tried Html.UrlEncode, Uri.EscapeDataString, Uri.EscapeUrlString, but none of those produces the desirable "c%23" format.

So how should I encode the tagName for the anchor?

+3  A: 

It seems like you're doing two encodes of your URL as c%2523 is the encoded value for c%23. (0x23 = ASCII 35 = #, 0x25 = ASCII 37 = %). As you have two encode calls (Html.Encode and Html.UrlEncode), perhaps removing one of them removes your problem.

schnaader
if I don't escape it at all, it outputs c# unescaped, and the hash becomes the url fragment
gCoder
The first Html.Encode is for the link text, so it has nothing to do with the href part of the anchor
gCoder
Yes, also noticed that. I haven't used ASP yet, so I can only guess. By the way, can you try something like 'new {tagName="c%23"}' for testing purposes?
schnaader
Hmm... good test. I put "c%23" manually in there and it still escaped it with "c%2523". But if I put "c#" it doesn't escape it at all. Seems like there's some kind of "logic" inside the Html.ActionLink helper class in there.
gCoder
There is a similar question here: http://stackoverflow.com/questions/406711/urlencode-of-link-ids-with-asp-net-mvc-htmlhelper-extension-methods - but there is no solution to it, so it won't help much.
schnaader
I did a quick peek into the mvc source code, and it seems they are doing Html.AttributeEncode (for the "href" part) before it gets output. Maybe that's what is causing the trouble. I may skip the ActionLink helper altogether if I don't find a way around it. Thanks.
gCoder
A: 

# corresponds to the ascii value of 23 in hex. So I guess they use the hex value.

Brian R. Bondy
A: 

If i recal correctly, then will actionlink automaticly encode the values on the url, so just remove the Html.Encode(tagName), and it should look nicely.

Jesper Blad Jensen aka. Deldy
A: 
<%= Html.ActionLink(Html.Encode(tagName), 
"tagged" //action,  
"posts" //controller, 
new {tagName=Html.UrlEncode(tagName)} //querystring argument, 
new {@class="tag"} //html attributes) 
%>

Html.ActionLink creates HTML and deals with the escaping issues for you. You should not explicitly Html.Encode the link text, or Html.UrlEncode the parameter.

If a ‘#’ in a parameter value is not being escaped that sounds like a bug in ActionLink() and you might have to generate the link yourself.

bobince
I saw that it Html.AttributeEncode() all attributes of the link, so there lies the problem. The "c#" string won't be escaped as it's a valid attribute string, but if I URL-encode "c#" first, it will then AttributeEncode() the already escaped string. I have to skip the helper altogether in this case
gCoder
bobince
Well clearly the ActionLink helper is doing "something" in there that messes it up. I skipped it and used "<a href="...">...</a>" and I did my work..
gCoder