Question

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!

Was it helpful?

Solution

You are using AppendLine, which is adding in carriage returns (new lines) to your string, this can be seen with the %0d in your url string.

Simply change your logic to use the string builder Append method.

Example would be:

if (!QueryString1.IsNullOrEmpty())
{
    SB.Append(QueryString1);
}

edit: or if you want to encode only the parameters.

if (!QueryString1.IsNullOrEmpty())
{
    SB.Append(HttpUtility.UrlEncode(QueryString1));
}

I think it can be done either way, the docs at http://msdn.microsoft.com/en-us/library/h10z5byc.aspx say it can be used to encode the entire URL

OTHER TIPS

You should be calling HttpUtility.UrlEncode() on each individual value of the query string, rather than on the whole string. So something like

SB.Append("&name=" + HttpUtility.UrlEncode(value));

You are encoding all the url, you should only encode the parameters values, so as you have coded it, you must encode those values before you set in QueryStringX and don't encode anything else.

Or a better approach, do it in your QueryStringX, when set is called, split th value by "=", encode the second part of the result, join them in one string and store it for later use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top