Ampersand (&) in QueryString

E

Elroyskimms

I have to encode an address which contains an ampersand (&) into a URL
with various querystring parameters. The following code works fine:

URLString = "www.myserver.com?AD1=" & HTTPUtility.URLEncode("500 Rt 6 &
209")
and then to retrieve it:
txtAddress1.text = Request.Querystring("AD1")

However, in a different page of code, I loop through the
QueryString.AllKeys collection like this:

URLString = "www.myserver.com?AD1=" & HTTPUtility.URLEncode("500 Rt 6 &
209")
and then to retrieve it:
Dim QStrings As New
System.Collections.Specialized.NameValueCollection(Request.QueryString)
Dim KeyArray() As String = Request.QueryString.AllKeys
Dim ValuesArray() As String
For i = 0 To Request.QueryString.Count - 1
ValuesArray = QStrings.GetValues(i)
Select Case KeyArray(i).ToUpper
Case "AD1"
txtAddress1.Text = ValuesArray(0)

In this code, the AD1 value is terminated at the ampersand. So,
txtAddress1.text is set to "500 Rt 6 ". Even worse is that an extra
Name/Value pair is inserted into the collection after the AD1
name/value pair but there is no Key name assigned to it. So, when
looping through the collection, the Select Case KeyArray(i).ToUpper
line bombs because the "name" of the key is NULL (nothing) but the
value is the last part of the address, " 209".

Why is it that when using the QueryString name/value collection,
encoded ampersands are decoded and treated as starting a new parameter?

Thanks in advance,

-E
 
R

Rad [Visual C# MVP]

I have to encode an address which contains an ampersand (&) into a URL
with various querystring parameters. The following code works fine:

URLString = "www.myserver.com?AD1=" & HTTPUtility.URLEncode("500 Rt 6 &
209")
and then to retrieve it:
txtAddress1.text = Request.Querystring("AD1")

However, in a different page of code, I loop through the
QueryString.AllKeys collection like this:

URLString = "www.myserver.com?AD1=" & HTTPUtility.URLEncode("500 Rt 6 &
209")
and then to retrieve it:
Dim QStrings As New
System.Collections.Specialized.NameValueCollection(Request.QueryString)
Dim KeyArray() As String = Request.QueryString.AllKeys
Dim ValuesArray() As String
For i = 0 To Request.QueryString.Count - 1
ValuesArray = QStrings.GetValues(i)
Select Case KeyArray(i).ToUpper
Case "AD1"
txtAddress1.Text = ValuesArray(0)

In this code, the AD1 value is terminated at the ampersand. So,
txtAddress1.text is set to "500 Rt 6 ". Even worse is that an extra
Name/Value pair is inserted into the collection after the AD1
name/value pair but there is no Key name assigned to it. So, when
looping through the collection, the Select Case KeyArray(i).ToUpper
line bombs because the "name" of the key is NULL (nothing) but the
value is the last part of the address, " 209".

Why is it that when using the QueryString name/value collection,
encoded ampersands are decoded and treated as starting a new parameter?

Thanks in advance,

-E

Shouldn't you URLDecode before processing?
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top