开发者

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&amp开发者_开发技巧;Key=%2fwEUKwEDAgQCFAJA


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.


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 &amp; 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 &amp; will pass:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜