how to properly encode ampersands in querystring

D

darrel

I am creating a querystring to look like this:

form_edit.aspx?collectionID=25&confirmationMessage=New+form+entry+saved

Note that I'm escaping the ampersand.

However, I can't grab the 'confirmationMessage' querystring unless I do NOT
encode the ampersand. I assume I'm missing something obvious here. Any
suggestions?

-Darrel
 
T

tom.pesterDeLeTeTHISSS

I'm curious why do you need to encode the ampersand? Is it to comply to XHTML
standards?

Cheers,
Tom Pester
 
G

Guest

I suggest that you put one more ampersand after & because each and every
value in a query string is separated by ampersand. For example
index.aspx?a=5&b=10. In your example,
form_edit.aspx?collectionID=25&&confirmationMessage=New+....

I hope it helps to encode/decode.
 
K

Kevin Spencer

You're using HTML Encoding in a URL. You should use URL Encoding instead:

form_edit.aspx?collectionID=25%38confirmationMessage=New+form+entry+saved

OR, you should NOT encode the ampersand at all, if you're trying to send
multiple parameters:

form_edit.aspx?collectionID=25&confirmationMessage=New+form+entry+saved

The ampersand is a standard URL delimiter for parameters in a query string.
It should never be HTML-Encoded. If you want to send a literal ampersand,
you should use URL-Encoding, as per my first suggestion.


--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
O

Oenone

Kevin said:
The ampersand is a standard URL delimiter for parameters in a query
string. It should never be HTML-Encoded.

Unless it's being included within the HTML itself (for example, in an anchor
tag), in which case it should.

This is valid XHTML (and works just fine):

<a href="http://myserver/mypage.aspx?a=1&amp;b=2">Link</a>

When clicked, the actual URL that will be present in the browser will have a
plain ampersand between the two parameters, not an encoded ampersand.

This is not valid (though it also works):


<a href="http://myserver/mypage.aspx?a=1&b=2">Link</a>

HTML can be a pain in the backside sometimes.
 
D

darrel

I'm curious why do you need to encode the ampersand? Is it to comply to
XHTML
standards?

I don't *need* too...just trying to get in the habit of doing it.

-Darrel
 
D

darrel

I suggest that you put one more ampersand after & because each and every
value in a query string is separated by ampersand. For example
index.aspx?a=5&b=10. In your example,
form_edit.aspx?collectionID=25&amp;&confirmationMessage=New+....

I hope it helps to encode/decode.

The problem is that I still have an unencoded ampersand in the URL then.

-Darrel
 
D

darrel

The ampersand is a standard URL delimiter for parameters in a query
string.
It should never be HTML-Encoded. If you want to send a literal ampersand,
you should use URL-Encoding, as per my first suggestion.

I was under the impression that an amersand in a URL, unenecoded, was
invalid XHTML.

http://www.456bereastreet.com/archive/200406/ampersands_and_validation/

Also, you introduce the problem of accidently creating an unintentional
entity:

mypage.aspx?id=2&amplitude=4
 
D

darrel

This is valid XHTML (and works just fine):

<a href="http://myserver/mypage.aspx?a=1&amp;b=2">Link</a>

When clicked, the actual URL that will be present in the browser will have a
plain ampersand between the two parameters, not an encoded ampersand.

Ah! Perhaps that's my problem. I'm not creating an href link, but rather,
I'm using response.redirect

-Darrel
 
K

Kevin Spencer

A URL is not HTML or XHTML. It is a URL. I was speaking in the context of a
URL, not the HTML document itself. When a URL is inside an HTML document (in
a link, for example), it needs to be HTML-Encoded. Otherwise not. See
http://www.gbiv.com/protocols/uri/rfc/rfc3986.html

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
O

Oenone

darrel said:
Ah! Perhaps that's my problem. I'm not creating an href link, but
rather, I'm using response.redirect

Right, in that case, you don't want to encode ampersands when you're using
them as value separators.

You'd only encode them in a Response.Redirect if they actually formed part
of one of the values, as detailed somewhere else on this thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top