Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Building a System.Uri with a query string

Building a System.Uri with a query string

Scheduled Pinned Locked Moved C#
questioncsharphtmldatabasevisual-studio
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Spacix One
    wrote on last edited by
    #1

    I give up, this seems impossible without the UriBuilder class... Well since .NET 1.1 when they made Uri(string, bool) obsolete... So here is my question: How do you build a System.Uri object in .NET compact framework that points to a "file://" scheme with passed query information? I've tried every valid (not obsolete) constructor for System.Uri meaning: * Uri(string) * Uri(string, UriKind) * Uri(base, string) * Uri(Uri, Uri) It didn't matter which I use because they don't have any affect on the outcome... Every constructor will escape the ? to hex as %3F which is not valid to use with query information!!! I can do this fine with strings but if I pass a string to the WebBrowser control it'll convert the string to a Uri object and have the problem again. So I need to build the correct Uri object and pass it to the WebBrowser control. MSDN says the following lie:

    http://msdn.microsoft.com/en-us/library/system.uri(VS.80).aspx[^] wrote:

    The Uri constructors do not escape URI strings if the string is a well-formed URI including a scheme identifier.

    Proven by this example:(start a new smart device application and add this code to the form1.cs file)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace DeviceApplication1
    {
    public partial class Form1 : Form
    {
    public int demoValue = 4;
    public Form1()
    {
    InitializeComponent();
    this.Load += new EventHandler(this.Form1_Load);
    }

        void Form1\_Load(object sender, EventArgs e) 
        { 
            Uri htmlFile = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), UriKind.Absolute); 
            MessageBox.Show("htmlFile.AbsoluteUri=\\r\\n" + htmlFile.AbsoluteUri); //show how the path is converted correctly into a uri string with a correct scheme 
            string properFormated = string.Format("{0}/DeviceApplication1/test.html?test={1}", htmlFile.AbsoluteUri, this.demoValue); 
            MessageBox.Show("properFormated=\\r\\n" + properFormated); //show the string is properly formated and escaped correctly! 
            Uri fil
    
    J 1 Reply Last reply
    0
    • S Spacix One

      I give up, this seems impossible without the UriBuilder class... Well since .NET 1.1 when they made Uri(string, bool) obsolete... So here is my question: How do you build a System.Uri object in .NET compact framework that points to a "file://" scheme with passed query information? I've tried every valid (not obsolete) constructor for System.Uri meaning: * Uri(string) * Uri(string, UriKind) * Uri(base, string) * Uri(Uri, Uri) It didn't matter which I use because they don't have any affect on the outcome... Every constructor will escape the ? to hex as %3F which is not valid to use with query information!!! I can do this fine with strings but if I pass a string to the WebBrowser control it'll convert the string to a Uri object and have the problem again. So I need to build the correct Uri object and pass it to the WebBrowser control. MSDN says the following lie:

      http://msdn.microsoft.com/en-us/library/system.uri(VS.80).aspx[^] wrote:

      The Uri constructors do not escape URI strings if the string is a well-formed URI including a scheme identifier.

      Proven by this example:(start a new smart device application and add this code to the form1.cs file)

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;

      namespace DeviceApplication1
      {
      public partial class Form1 : Form
      {
      public int demoValue = 4;
      public Form1()
      {
      InitializeComponent();
      this.Load += new EventHandler(this.Form1_Load);
      }

          void Form1\_Load(object sender, EventArgs e) 
          { 
              Uri htmlFile = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), UriKind.Absolute); 
              MessageBox.Show("htmlFile.AbsoluteUri=\\r\\n" + htmlFile.AbsoluteUri); //show how the path is converted correctly into a uri string with a correct scheme 
              string properFormated = string.Format("{0}/DeviceApplication1/test.html?test={1}", htmlFile.AbsoluteUri, this.demoValue); 
              MessageBox.Show("properFormated=\\r\\n" + properFormated); //show the string is properly formated and escaped correctly! 
              Uri fil
      
      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Also from the documentation:

      "The URI represented by a Uri instance is always in "escaped" form. The following characters are reserved:

      • Semi-colon (";" )

      • Forward slash ( "/")

      • Question mark ( "?" )

      • Colon ( ":" )

      • At-sign ("@")

      • Ampersand ( "&" )

      • Equal sign ("=" )

      • Plus sign ("+" )

      • US Dollar sign ("$" )

      • Comma (",")

      To transform the URI contained in a Uri instance from an escape encoded URI to a human-readable URI, use the System.Uri.ToString method."

      Another thing you could do is call Uri.UnescapeDataString(someString), which will give you back your URI with the proper unescaped characters.

      Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango

      S 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Also from the documentation:

        "The URI represented by a Uri instance is always in "escaped" form. The following characters are reserved:

        • Semi-colon (";" )

        • Forward slash ( "/")

        • Question mark ( "?" )

        • Colon ( ":" )

        • At-sign ("@")

        • Ampersand ( "&" )

        • Equal sign ("=" )

        • Plus sign ("+" )

        • US Dollar sign ("$" )

        • Comma (",")

        To transform the URI contained in a Uri instance from an escape encoded URI to a human-readable URI, use the System.Uri.ToString method."

        Another thing you could do is call Uri.UnescapeDataString(someString), which will give you back your URI with the proper unescaped characters.

        Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango

        S Offline
        S Offline
        Spacix One
        wrote on last edited by
        #3

        Well I need the Uri object to pass to the WebBrowser control. If you pass it a string it'll convert it to a Uri object thus escaping the string it will navigate to. In the Normal framework you can use UriBuilder to make a URL with extra unescaped information that is parsed properly by the .PathAndQuery property. Example: (use a new c# console project on the normal framework 2.0 or higher)

        using System;
        using System.Collections.Generic;
        using System.Text;

        namespace ConsoleApplication1
        {
        class Program
        {
        static void Main(string[] args)
        {
        int demoValue = 4;
        UriBuilder ub = new UriBuilder("file://", "", -1, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/DeviceApplication1/test.html", string.Format("?test={0}", demoValue));
        Console.WriteLine("Correct OutPut:\r\n{0}", ub.ToString()); //show path and query content (without errors)
        Console.ReadLine();
        }
        }
        }


        -Spacix All your skynet questions[^] belong to solved


        I dislike the black-and-white voting system on questions/answers. X|


        J 1 Reply Last reply
        0
        • S Spacix One

          Well I need the Uri object to pass to the WebBrowser control. If you pass it a string it'll convert it to a Uri object thus escaping the string it will navigate to. In the Normal framework you can use UriBuilder to make a URL with extra unescaped information that is parsed properly by the .PathAndQuery property. Example: (use a new c# console project on the normal framework 2.0 or higher)

          using System;
          using System.Collections.Generic;
          using System.Text;

          namespace ConsoleApplication1
          {
          class Program
          {
          static void Main(string[] args)
          {
          int demoValue = 4;
          UriBuilder ub = new UriBuilder("file://", "", -1, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/DeviceApplication1/test.html", string.Format("?test={0}", demoValue));
          Console.WriteLine("Correct OutPut:\r\n{0}", ub.ToString()); //show path and query content (without errors)
          Console.ReadLine();
          }
          }
          }


          -Spacix All your skynet questions[^] belong to solved


          I dislike the black-and-white voting system on questions/answers. X|


          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          It's amazing I've gone so long without knowing about UriBuilder. Thanks for that. So your issue is only with the .NET Compact Framework? Interesting. I'm afraid I don't have an answer for this. Have you tried asking at the Mobile Development[^] forums? If this turns out to be a bug in the .NET Compact Framework, you ought to report this to the Microsoft Connect site.

          Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango

          S 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            It's amazing I've gone so long without knowing about UriBuilder. Thanks for that. So your issue is only with the .NET Compact Framework? Interesting. I'm afraid I don't have an answer for this. Have you tried asking at the Mobile Development[^] forums? If this turns out to be a bug in the .NET Compact Framework, you ought to report this to the Microsoft Connect site.

            Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango

            S Offline
            S Offline
            Spacix One
            wrote on last edited by
            #5

            Yes, UriBilder is handy as StringBuilder because Uri like String objects are immutable reference types, so if it is changed a new one is made. Hmmm hadn't though about the Mobile Dev forum (never frequent there) I sort of assumed it was for native code, it'll be worth a shot I guess :D Though I do hate to double post this :( I do have MSDN support issues left over from the VS2k8 upgrage, think they give bug reports back ;) Not like I'd use them anyway... maybe a MVP might look at this :rose:


            -Spacix All your skynet questions[^] belong to solved


            I dislike the black-and-white voting system on questions/answers. X|


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups