none
Uri.TryCreate Method unescapes relativeUri -- Why? RRS feed

  • Question

  • I am using System.Uri.TryCreate (http://msdn.microsoft.com/en-us/library/ms131573.aspx) to create a URL from a base URL and a relative URL.  I just want to do the same thing that the browser does when it tries to resolve an anchor tag.

     

    My specific values are:

      baseUri: http://localhost:36695/Default.aspx

      relativeUri: Foo/Bar_11%2F13%2F2009%20(5156)

     

      returnValue: http://localhost:36695/Foo/Bar_11/13/2009 (5156)

     

    What is baffling me is that it seems the second URL is getting decoded before appending to the first URL.  Do you know if this is a bug in .Net, or if there is another API I can use to get what I want?  What I want is this:

     

    http://localhost:36695/Foo/Bar_11%2F13%2F2009%20(5156)

    Thanks.

    Monday, November 23, 2009 9:49 PM

Answers

  • Hi,
    You can try the OriginalString property of type Uri, something like this:
                    Uri u;
                    Uri.TryCreate(new Uri("http://localhost:36695/Default.aspx"), "Foo/Bar_11%2F13%2F2009%20(5156)", out u);
                    Console.WriteLine(u.OriginalString);


    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Tuesday, November 24, 2009 5:50 AM

All replies

  • Hi,
    You can try the OriginalString property of type Uri, something like this:
                    Uri u;
                    Uri.TryCreate(new Uri("http://localhost:36695/Default.aspx"), "Foo/Bar_11%2F13%2F2009%20(5156)", out u);
                    Console.WriteLine(u.OriginalString);


    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Tuesday, November 24, 2009 5:50 AM
  • Thanks.  That does what I want. :-D
    Wednesday, November 25, 2009 4:47 PM