Monday, February 10, 2020

Relative Uris and leading/trailing backslash

Be very accurate with leading and trailing backslashes in URLs. For instance, I have some app installed on http://some.host/path that provides api via relative uri /api/some/call

There is only one correct combination of Base and Rel Uris:

(4 items)4
Base
Rel
Result
http://some.host/path/
/api/some/call
http://some.host/api/some/call
http://some.host/path/
api/some/call
http://some.host/path/api/some/call
http://some.host/path
/api/some/call
http://some.host/api/some/call
http://some.host/path
api/some/call
http://some.host/api/some/call

var baseUrl = new Uri("http://some.host/path/");
var relUrl = new Uri("api/some/call", UriKind.Relative);

var result = new Uri(baseUrl, relUrl);


So as thumb rules: All base urls should be with trailing backslashes and all relative urls should be without trailing backslashes.