locked
UrlPathEconde not returning the correct string RRS feed

  • Question

  • User-331387351 posted

    I have the following piece of code which constructs the PayPal CheckOut link. And on the click event, I <g class="gr_ gr_153 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="153" data-gr-id="153">am  calling</g> this function that is shown below.

    The string formatting appears to be incorrectly displayed for the currency. The output generated is given below. Have a look at the text before GBP.

    <g class="gr_ gr_213 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="213" data-gr-id="213">http</g>://www.paypal.com/<g class="gr_ gr_214 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="214" data-gr-id="214">cgi</g>-bin/webscr?cmd_cart&business=resourcemapper2018@gmail.com&return=www.example.com&cancel_return=www.example.com¤cy=GBP&shopping_url=Abc&item_name=sss&amount=2&add=1

    Thanks for your help. 

    public static string ToPayPalAddItem(string productUrl, string productName, decimal productPrice, string productOptions)
    {
    return HttpUtility.UrlPathEncode(string.Format("{0}&business={1}&return={2}&cancel_return={3}&currency={4}&shopping_url={5}&item_name={6}&amount={7}&add=1",
    UFCFUD60M.Common.CPayPalConfiguration.PayPalUrl, //0
    UFCFUD60M.Common.CPayPalConfiguration.PayPalEMail, //1
    UFCFUD60M.Common.CPayPalConfiguration.PayPalReturnUrl, //2
    UFCFUD60M.Common.CPayPalConfiguration.PayPalCancelUrl, //3
    UFCFUD60M.Common.CPayPalConfiguration.PayPalCurrency, //4
    productUrl, //5
    productName, //6
    productPrice,
    productOptions

    ));
    }

    namespace UFCFUD60M.Common
    {
    public class CPayPalConfiguration
    {

    private readonly string zPayPalUrl;

    public static string PayPalUrl
    {
    get
    {

    return ConfigurationManager.AppSettings["PayPalUrl"];
    }
    }

    public static string PayPalEMail
    {
    get
    {
    return ConfigurationManager.AppSettings["PayPalEMail"];
    }
    }

    public static string PayPalCurrency
    {
    get
    {
    return ConfigurationManager.AppSettings["PayPalCurrency"];
    }
    }

    public static string PayPalReturnUrl
    {
    get
    {
    return ConfigurationManager.AppSettings["PayPalReturnUrl"];
    }
    }


    public static string PayPalCancelUrl
    {
    get
    {
    return ConfigurationManager.AppSettings["PayPalCancelUrl"];
    }
    }
    }
    }

    <!--
    Paypal Settinsgs
    -->
    <add key="PayPalUrl" value="http://www.paypal.com/cgi-bin/webscr?cmd_cart"/>
    <add key="PayPalEMail" value="resourcemapper2018@gmail.com"/>
    <add key="PayPalCurrency" value="GBP"/>
    <add key ="PayPalReturnUrl" value="www.example.com"/>
    <add key="PayPalCancelUrl" value="www.example.com"/>
    <!--
    End of PayPal Settings
    -->


    <!--

    Saturday, January 5, 2019 1:16 AM

All replies

  • User-1174608757 posted

    Hi Aryaman,

    According to your description, I suggest you to use  HttpUtility.UrlEncode( ) not UrlPathEncode( ).

     HttpUtility.UrlEncode( ) will both encode the url  and querystring parameter but UrlPathEncode( ) will only encode the url part.

    Since you want  to encode the Querystring part , It will show the result you want by using   HttpUtility.UrlEncode( ).

    Here is a demo ,I hope it can help you.

    aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </div>
        </form>
    </body>

    aspx.cs:

    public partial class encode : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text = HttpUtility.UrlPathEncode("http://www.paypal.com/cgi-bin/webscr?cmd_cart&business=resourcemapper2018@gmail.com");
                Label2.Text = HttpUtility.UrlEncode("http://www.paypal.com/cgi-bin/webscr?cmd_cart&business=resourcemapper2018@gmail.com");
            }
        }

    It shows as below:

    Best Regards

    Wei Zhang

    Monday, January 7, 2019 7:29 AM
  • User1813023915 posted

    For developers who have to use the classic ASP.NET, I have created a manual.
    PayPal Demo

    Wednesday, February 12, 2020 11:50 AM