views:

3719

answers:

5

My website is XHTML Transitional compliant except for one thing: the & (ampersand) in the URL are written as it is, instead of &

That is, all my urls in pages are as usual as:

<a href="http://www.foo.com/page.aspx?x=1&amp;y=2"&gt;Foo&lt;/a&gt;

But XHTML validator generets the error

cannot generate system identifier for general entity "y"

wants the url is

<a href="http://www.foo.com/page.aspx?x=1&amp;amp;y=2"&gt;Foo&lt;/a&gt;

The problem is that IE and Firefox don't handle the URL correctly and ignore the y parameter. How to do? It seems to me that is impossible to write XHTML pages if the browsers don't work with strict encoded XHTML URLs.

Do you want to see in action? See the difference between (copy and paste as it is):

http://stackoverflow.com/search?q=ff&amp;sort=newest

and

http://stackoverflow.com/search?q=ff&amp;amp;sort=newest

Any help?

+4  A: 

you could use &amp; instead of & in your url within your page.

That should allow it to be validated a strict xhtml...

<a href="http://www.foo.com/page.aspx?x=1&amp;amp;y=2"&gt;Foo&lt;/a&gt;

Note, if used by an ASP.NET Request.QueryString function, the query string doesn't use XML encoding, it uses URL encoding:

/mypath/mypage?b=%26stuff

So you need to provide a function translating '&' into %26

Note: in that case, Server.URLEncode(”neetu & geetu”), which would produce neetu+%26+geetu, is not what you want, since you need to translate & into %26, not just '&'. You must add a replace() call applied to URLEncode result, in order to replace '%26amp;' by '%26'.

VonC
Sorry, I've replied you with a new answer instead of a comment... see my answer if you want to follow the thread
Got your comment just now ;) Checking into this issue
VonC
A: 

Hi VonC,

yes I can do it, and I obtain valid XHTML pages. But ASP.NET doesn't work anymore, because the Request.QueryString don't recognize the parameter y but the amp;y parameter! So my website don't work anymore!

Is there a way to get normal parameters and render valid xhtml pages in asp.net?

Matthew Scharley
You should have answered as comment, so VonC would have been notified...He is right, that's the correct way of writing URLs. ASP.NET should provide an encoding function, you should check it.
PhiLho
Konrad Rudolph
added remark on URL encoding part. Could you check it out ?
VonC
+9  A: 

I have just tried this, what you attempted to do is correct. In HTML if you are writing a link the & characters should be encoded as &amp; You would only encode the & as %26 if you wanted a parameter value to contain an ampersand. I just wrote a simple html page that contained a link
<a href="Default2.aspx?param1=63&amp;param2=hel">Click me</a>
and it worked fine, default2.aspx received the parameters intended and the source passed validation.

The encoding of & as &amp; is required in HTML, not in the link. When the browser sees the &amp; in the HTML source for a link it will interpret it as an ampersand and the link target will be as intended. If you paste a URL into your browser address bar it does not expect it to be HTML and does not try to interpret any HTML encoding that it may contain. This is why your example links that you suggest we should copy/paste into a browser don't work and why we wouldn't expect them to work.

If you post a bit more of your actual code we might be able to see what you have done wrong, but you appear to be heading the right direction by using &amp; in your anchor tags.

pipTheGeek
+3  A: 

It was my fault: the Hyperlink control already encoded &, so my url http://foo?x=1&amp;amp;y=2 was encoded to http://foo?x=1&amp;amp;amp;y=2

Normally the &amp inside the url is correctly handled by browsers, as you stated. Thanks

A: 

The problem is worse than you think - try it in Safari. &amp; gets converted to &#38; and the hash ends the URL. The correct answer is to not output XHTML - there's no reason that justifies spending more time on development and alienating mac users.

Simon