Middletone
Middletone

Reputation: 4270

How do I prevent ASP.NET from converting the & symbol to & when adding a url to a control's attributes?

This is what I'm adding and it renders the & as &

sharelink.Attributes.Add("addthis:url", "http://" & Request.Url.Host & "/Resources/PublicView.aspx?RID=" & R.ResourceID & "&Key=" & Key)

I need it to render this

?RID=64&Key=%2fwEUKwEDAgQCFAJA

instead of this

?RID=64&Key=%2fwEUKwEDAgQCFAJA

Upvotes: 3

Views: 2195

Answers (2)

o.k.w
o.k.w

Reputation: 25810

I post this comment as an answer due to limitation in commenting.

I agree with the answer by SLaks, this is especially so for strict XHTML compliance.

I've created a demo page with both & and & which can be used for W3C validation.

See the demo page at http://74er.net/labs/amp-or-not.html and click on the bottom-most hyperlink to perform a w3c validation, or click here to validate directly.

The errors will be highlighted, all targetted at the line 14:

href="http://www.google.com/search?q=stackoverflow&ie=UTF-8"

Whereas a similar one at line 19 with & will pass:

href="http://www.google.com/search?q=stackoverflow&ie=UTF-8"

Upvotes: 1

SLaks
SLaks

Reputation: 887767

Converting to & is correct, and the browsers should be able to handle it.

Source

To prove it, look at the source for this hyperlink.

EDIT: To explain, this is the correct way to do it. Writing <a href="a.com?b=c&d=e"> is wrong. Just like ASP.Net will use entities if you write <asp:Label runat="server" Text="><><" />, it will also use entities if you put an ampersand in an attribute.

Upvotes: 2

Related Questions