tags:

views:

32

answers:

2

I'm seeing some strange (to me anyway) behavior when using MakeRelativeUri on Mono (2.6.7). Take the following example:

var uri1 = new Uri("/somepath/someothersubpath/");
var uri2 = new Uri("/somepath/img/someimg.jpg");

var uri3 = uri1.MakeRelativeUri(uri2);
Console.WriteLine(uri3.OriginalString);

I'd expect this to output "../img/someimg.jpg", but I get "img/someimg.jpg"

A friend has confirmed using windows/visual studio that he gets my expected result if appending an extra slash to the beginning of the string (I've tried this as well, and to no avail).

I'm not sure if this is an issue with the Uri class in mono, or if my understanding of how the URI class should work is flawed, but any advice that can help me get to the expected output would be greatly appreciated.

Thanks,

Alex

+1  A: 

It seems like it's making it relevant to someothersubpath itself, not to its children.

I'm not sure about it, but maybe it's possible to get around this by appending anything to the first string:

var uri1 = new Uri("/somepath/someothersubpath/anything");
var uri2 = new Uri("/somepath/img/someimg.jpg");

var uri3 = uri1.MakeRelativeUri(uri2);
Console.WriteLine(uri3.OriginalString);
aularon
Interesting. That does seem to solve my problem, but it does seem like it may be an issue with the URI class in mono. Thanks for the help, hopefully we'll be able to figure out the cause of this eventually
AlexCuse
It's due to that `/somepath/img/someimg.jpg` is `img/someimg.jpg` from `/somepath/someothersubpath` point of view. But adding a trailing slash to that path making it `/somepath/someothersubpath/` should mean that we are inside of that directory, but it seems `Mono` ignores this, maybe a bug. However, adding a new component to the end of the path forces it to change to a _deeper point of view_ :)
aularon
Yeah, it does look like mono is ignoring it. Adding another slash works as well, maybe it is just trimming off an ending slash?
AlexCuse
If adding another slash solved it too, yes that is probably the case.
aularon
A: 

The Microsoft .NET documentation for Uri.MakeRelativeUri says that it should throw InvalidOperationException for relative Uris.

Your code, as written, throws an exception on the first line: "Invalid URI: The format of the URI could not be determined." If I modify the code:

    var uri1 = new Uri("/somepath/someothersubpath/", UriKind.RelativeOrAbsolute);
    var uri2 = new Uri("/somepath/img/someimg.jpg", UriKind.RelativeOrAbsolute);

    var uri3 = uri1.MakeRelativeUri(uri2);

Then the last line throws InvalidOperationException: This operation is not supported for a relative URI. Just as the documentation says it should.

So it appears that the Mono implementation is at odds with the .NET documentation.

Jim Mischel
Sorry, seems I should've been more clear. I was using UriKind.Absolute but since results were the same either way I left it out. In this case I'm actually resolving to the filesystem (where these are absolute paths) as well.
AlexCuse
It's rather difficult to give informed and relevant answers when critical information is left out.
Jim Mischel