Create new URI from Base URI and Relative Path - slash makes a difference?
Why does a slash make difference when using new URI(baseUri, relativePath)?
Well, that's what happens on the web normally.
For example, suppose I'm looking at http://foo.com/some/file1.html
and there's a link to file2.html
- that link goes to http://foo.com/some/file2.html
, right? Not http://foo.com/some/file1.html/file2.html
.
More specifically though, this follows section 5.2.3 of RFC 3986.
5.2.3. Merge Paths
The pseudocode above refers to a "merge" routine for merging a relative-path reference with the path of the base URI. This is accomplished as follows:
If the base URI has a defined authority component and an empty path, then return a string consisting of "/" concatenated with the reference's path; otherwise,
return a string consisting of the reference's path component appended to all but the last segment of the base URI's path (i.e., excluding any characters after the right-most "/" in the base URI path, or excluding the entire base URI path if it does not contain any "/" characters).
I've been playing around with the Uri constructor with the overload new Uri(baseUri, relativePath)
. Perhaps others may find the results useful. Here's the output from the test application I wrote:
A) Base Address is domain only
==============================
NO trailing slash on base address, NO leading slash on relative path:
http://foo.com + relative1/relative2 :
http://foo.com/relative1/relative2
NO trailing slash on base address, relative path HAS leading slash:
http://foo.com + /relative1/relative2 :
http://foo.com/relative1/relative2
Base address HAS trailing slash, NO leading slash on relative path:
http://foo.com/ + relative1/relative2 :
http://foo.com/relative1/relative2
Base address HAS trailing slash, relative path HAS leading slash:
http://foo.com/ + /relative1/relative2 :
http://foo.com/relative1/relative2
B) Base Address includes path
=============================
NO trailing slash on base address, NO leading slash on relative path:
http://foo.com/base1/base2 + relative1/relative2 :
http://foo.com/base1/relative1/relative2
(removed base2 segment)
NO trailing slash on base address, relative path HAS leading slash:
http://foo.com/base1/base2 + /relative1/relative2 :
http://foo.com/relative1/relative2
(removed base1 and base2 segments)
Base address HAS trailing slash, NO leading slash on relative path:
http://foo.com/base1/base2/ + relative1/relative2 :
http://foo.com/base1/base2/relative1/relative2
(has all segments)
Base address HAS trailing slash, relative path HAS leading slash:
http://foo.com/base1/base2/ + /relative1/relative2 :
http://foo.com/relative1/relative2
(removed base1 and base2 segments)