locked
How to pass + in the url query string parameter? RRS feed

  • Question

  • User-1603557471 posted

    Hi there,

    We are passing some parameters (Server.UrlEncode) from one page to another via the url QueryString. However, in a case when there is a +, it disappeared, after Server.UrlDecode. Using Server.HtmlEncode and Server.HtmlDecode produce the same result.

    For eg, <t1> +2     becomes     <t1>  2

    Page 1:
    string msg = "<t1> +2";
    string encoded = Server.UrlEncode(Server.HtmlEncode(msg));
    StringBuilder url = new StringBuilder();
    url.Append("Default8a.aspx?P1=");
    url.Append(encoded);
    Response.Redirect(url.ToString(), true);

    Page 2:
    string p1 = Server.UrlDecode(Server.HtmlDecode(Request.QueryString["P1"])).ToString();

    How would you preserve the + sign?

    Thanks,

    Ben

    Thursday, August 2, 2012 5:31 AM

Answers

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 2, 2012 6:07 AM
  • User635950694 posted

    Hi Ben ,

    Try to use

    Server.UrlEncode("<t1> +2     becomes     <t1>  2");

    Please mark this post as Answer if this helped you.
    Thursday, August 2, 2012 6:08 AM
  • User-1603557471 posted

    Yes. That's it! Thank you so much :-)

    Thursday, August 2, 2012 6:45 AM
  • User1352203401 posted

    How would you preserve the + sign?

    Page 2:
    string p1 = Server.UrlDecode(Server.HtmlDecode(Request.QueryString["P1"])).ToString();

    Hi,

    Remove the Server.UrlDecode method from your code and check,

    string p1 = Server.HtmlDecode(Request.QueryString["P1"]);

    found from net,

    url code for plus symbol is %2b, if you decode it you will get "+" sumbol 

    but if you decode "+" symbol you will get space as a result. this is what happening in your case

    Thursday, August 2, 2012 6:53 AM
  • User-1603557471 posted

    Ben Teo

    How would you preserve the + sign?

    Ben Teo

    Page 2:
    string p1 = Server.UrlDecode(Server.HtmlDecode(Request.QueryString["P1"])).ToString();

    Hi,

    Remove the Server.UrlDecode method from your code and check,

    string p1 = Server.HtmlDecode(Request.QueryString["P1"]);

    found from net,

    url code for plus symbol is %2b, if you decode it you will get "+" sumbol 

    but if you decode "+" symbol you will get space as a result. this is what happening in your case

    Hi,

    Tried:

    1)
    Page 1:
    string encoded = Server.UrlEncode(msg);

    Page 2:
    string p1 = Server.UrlDecode(Request.QueryString["P1"]).ToString();

    2)
    Page 1:
    string encoded = Server.HtmlEncode(msg);

    Page 2:
    string p1 = Server.HtmlDecode(Request.QueryString["P1"]).ToString();

    produce the same result, ie. + becomes a blank.

    So following the link eg,  

    Page 1:

    Server.UrlEncode(msg.Replace("+", "%252b")); 

    Page 2:

    string p1 = Server.UrlDecode(Request.QueryString["P1"]).ToString();
    p1 = System.Text.RegularExpressions.Regex.Replace(p1, "%2b", "+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

    , then the + sign is preserved.

    Thanks for your helps :)

    Friday, August 3, 2012 6:19 AM