Ampersand in URI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akhiln
    New Member
    • Aug 2009
    • 2

    Ampersand in URI

    Hi,

    I'm having an issue with ampersands in a URI string.

    I'm trying to build a URI by:

    string href = "www.something. com/?=" + var1 + "&" + var2;
    webBrowser1.nav igate(new Uri (href));

    But href ends up being www.something.c om/?=var1var2 without the ampersand.

    What can I do to make this work?

    Thanks.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    That's odd... it's working just fine for me. Though I did have to put an "http://" in front of that url otherwise it would throw an exception when I tried to turn it into a Uri object.

    Code:
                int var1 = 12;
                int var2 = 5;
                string href = "http://www.something.com/?=" + var1 + "&" + var2;
    
                Uri uri = new Uri(href);
    
                webBrowser1.Navigate(uri);

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Well "www.something. com/?=var1&var2" is a bad format to begin with.
      There is nothing to the left of the = sign.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You should have an "=" between the variable's name and a value for the variable followed. Each variable should be separated by an "&".

        Your URI should look like:

        "http://www.something.c om?var1=somethi ng&var2=somethi ng2"

        Comment

        • akhiln
          New Member
          • Aug 2009
          • 2

          #5
          Here is my actual code: I have the URL formatting right, the ampersand is just not showing up.

          Code:
          String[] split = data.Split(',');
          String href = "http://www.something.com/test.php?reader=";
          href += split[1].Trim();
          href += "&";
          href += "id=";
          href += split[2].Trim();
          webBrowser1.Navigate(new Uri(href));
          StatusLabel.Text = href;

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I just tried it:

            Code:
            string src="";
            src += "http://www.something.com?";
            src += "var1";
            src += "=";
            src += "something";
            src += "&";
            src+="var2";
            src += "=";
            src+="something2";
            The "&" is placed in the string...

            Your "StatusLabe l" variable, is it an ASP.NET Label or TextBox?

            Comment

            Working...