2015-04-17 50 views
0

我想解決我的查詢字符串使用以下。有需要被顯示在地址例如井號:URL#issuie c#querystring asp.net

344座,裕廊西,街11#02-111

存儲在變量address。我想利用這個代碼來替換井號: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; 

但它仍然拋出一個錯誤。當我在查詢字符串中替換它時,它很好,數據顯示正常,所以這不是問題。

+0

記錯,你需要像[HttpUtility.UrlEncode方法](https://msdn.microsoft.com/en-us/library/system.web .httputility.urlencode(v = vs.110).aspx) – Grundy

+3

我建議你從網址中刪除所有這些數據,並使用表格instread – clement

+0

可以請你發佈你得到的異常嗎? – Rajesh

回答

0

使用Uri.EscapeDataString編碼所有查詢參數值:

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);