简体   繁体   中英

URL # issuie c# querystring asp.net

I'm trying to sort out my querystring using the following. There is a hash sign that needs to be displayed in the address for example:

Blk 344, Jurong West, Street 11, #02-111

Which is stored in the variable address . I am trying to use this code to replace the hash sign: address.Replace("#","%23")

string url = "thankyou.aspx?firstname=" + firstname + "&" + 
"lastname=" + lastname + "&" + "address=" +  address.Replace("#","%23")+ 
 "&" + "total=" + total + "&" + "nric=" + tbID.Text + "&" + "country=" + 
 ddlCountry.SelectedValue + "&" + "orderid=" + orderid + "&"+ 
"postalcode="+tbPostalCode.Text;

But it's still throwing an error. When I replace it in query string it is fine and the data is displayed fine, so this is not the issue.

Use Uri.EscapeDataString to encode all query parameter values :

string url = "thankyou.aspx?firstname=" + Uri.EscapeDataString(firstname)
            + "&lastname="   + Uri.EscapeDataString(lastname)
            + "&address="    + Uri.EscapeDataString(address)
            + "&total="      + Uri.EscapeDataString(total)
            + "&nric="       + Uri.EscapeDataString(tbID.Text)
            + "&country="    + Uri.EscapeDataString(ddlCountry.SelectedValue)
            + "&orderid="    + Uri.EscapeDataString(orderid)
            + "&postalcode=" + Uri.EscapeDataString(tbPostalCode.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM