I have some sample code where I want to build a query string but at the end of the code, the string is not encoded properly to be used in a browser.
public partial class UrlBuilder : System.Web.UI.UserControl
{
public string Title { get; set; }
public string Url { get; set; }
public bool EnableQryString { get; set; }
public string QueryString1 { get; set; }
public string QueryString2 { get; set; }
public string QueryString3 { get; set; }
public string QueryString4 { get; set; }
public string QueryString5 { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
lnkUrl.Text = Title;
lnkUrl.NavigateUrl = Url;
if (EnableQryString)
{
StringBuilder SB = new StringBuilder();
SB.AppendLine("?");
#region Query String builder
if (!QueryString1.IsNullOrEmpty())
{
SB.AppendLine(QueryString1);
}
if (!QueryString2.IsNullOrEmpty())
{
SB.AppendLine("&" + QueryString2);
}
if (!QueryString3.IsNullOrEmpty())
{
SB.AppendLine("&" + QueryString3);
}
if (!QueryString4.IsNullOrEmpty())
{
SB.AppendLine("&" + QueryString4);
}
if (!QueryString5.IsNullOrEmpty())
{
SB.AppendLine("&" + QueryString5);
}
#endregion
lnkUrl.NavigateUrl += HttpUtility.UrlEncode(SB.ToString());
}
}
}
This is the string after:
HttpUtility.UrlEncode(SB.ToString());
http://www.google.com%3f%0d%0aA%3d1%0d%0a%26B%3d2%0d%0a
I think I am not using UrlEncode properly. Can someone help?
Thank you!