8

I need to combine two URL's that both contain .Path information.

I would like to use Uri to give me possibility to .TryCreate(), so I can catch malformed URL's.

The problem that I'm facing is that the base URI path seems to be ignored when I merge the absolute and the relative URI:

Uri absoluteUri= new Uri("http://hostname/path/", UriKind.Absolute);
Uri relativeUri = new Uri("/my subsite/my page.aspx?my=query", UriKind.Relative);
Uri resultUri;
if (!Uri.TryCreate(absoluteUri, relativeUri, out resultUri))
      // handle errors

Output of the above is:

http://hostname/my%20subsite/my%20page.aspx?my=query

I would like it to be:

http://hostname/path/my%20subsite/my%20page.aspx?my=query

Is there a way to combine URLs that both contain path information using the Uri class?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anders Rask
  • 862
  • 6
  • 17

2 Answers2

16

Your relative URI should be relative, i.e., remove first slash (or add a period),

string relative = "/my subsite/my page.aspx?my=query";

Uri test1= new Uri(relative.Substring(1), UriKind.Relative); // without 'root'
Uri test2= new Uri("." + relative, UriKind.Relative);        // with 'current'

Working example:

Uri baseUri = new Uri("http://hostname/path/");
string relative = "/my subsite/my page.aspx?my=query";

Uri test1 = new Uri(baseUri, relative);              // original string
Uri test2 = new Uri(baseUri, relative.Substring(1)); // without 'root' character
Uri test3 = new Uri(baseUri, "." + relative);        // with 'current' character

Console.WriteLine(test1.OriginalString); // wrong
Console.WriteLine(test2.OriginalString); // right!
Console.WriteLine(test3.OriginalString); // right!

So, you probably should deal with your relative part like this:

if (relative.StartsWith("/"))
    relative = "." + relative;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 2
    did you try this? doesnt make any difference when i do. – Anders Rask Nov 25 '09 at 10:59
  • @anders, yes, works for me; I edit my answer with a complete example; please, take a look – Rubens Farias Nov 25 '09 at 12:13
  • 5
    Ah i found what made your code work and not mine: It seems that the absolute url must also end with / to make it work! I used string absoluteUrl = "http://hostname/path"; and it didn't work, but with both end slahs after "path" and no start slash before "my" it seems to work! This seems flaky so ill test a bit more, but you deserve an answer for this one :-) – Anders Rask Nov 25 '09 at 12:35
1

It could be better to check both baseUri and relativePath when using Uri.TryCreate():

var baseUri = absoluteUri.EndsWith('/') ? absoluteUri : absoluteUri + '/';
var relativeUri = relativePath.StartsWith('/') ? relativePath.Substring(1) : relativePath;

Refer to https://stackoverflow.com/a/58757870/5765982

However, the base Uri has to end with / and the relative has to NOT begin with /; otherwise it will remove the trailing part of the base Url