public Uri MakeRelativeUri(Uri uri)
{
if ((object)uri == null)
throw new ArgumentNullException(nameof(uri));
if (IsNotAbsoluteUri || uri.IsNotAbsoluteUri)
throw new InvalidOperationException(SR.net_uri_NotAbsolute);
// Note that the UserInfo part is ignored when computing a relative Uri.
if ((Scheme == uri.Scheme) && (Host == uri.Host) && (Port == uri.Port))
{
string otherPath = uri.AbsolutePath;
// Relative Path
string relativeUriString = PathDifference(AbsolutePath, otherPath, !IsUncOrDosPath);
// Relative Uri's cannot have a colon ':' in the first path segment (RFC 3986, Section 4.2)
if (CheckForColonInFirstPathSegment(relativeUriString)
// Except for full implicit dos file paths
&& !(uri.IsDosPath && otherPath.Equals(relativeUriString, StringComparison.Ordinal)))
relativeUriString = "./" + relativeUriString;
// Query & Fragment
relativeUriString += uri.GetParts(UriComponents.Query | UriComponents.Fragment, UriFormat.UriEscaped);
return new Uri(relativeUriString, UriKind.Relative);
}
return uri;
}