UriHelper.Combine Method

Namespace
Kampute.DocToolkit.Support
Assembly
  • Kampute.DocToolkit.dll

Definition

Creates a new Uri by combining the Uri with a relative URI string.
public static Uri Combine(this Uri baseUri, string relativeUri)

Parameters

baseUri Uri
The base Uri instance.
relativeUri string
The relative URI string to combine with the base URI.

Returns

Uri
A new Uri that represents the combination of the base Uri and the relative URI string.

Exceptions

ArgumentNullException
Thrown when baseUri is null.
ArgumentException
Thrown when relativeUri attempts to navigate beyond the root directory.

Remarks

This method combines the base Uri with a relative URI string according to the following rules:
  • PathIf the path of relativeUri starts with a forward slash, it replaces the base URI's path; otherwise, it is appended to the base URI's path.
  • QueryThe query string of the resulting URI is a combination of the base URI's query string and the relative URI's query string.
  • FragmentIf relativeUri contains a fragment, it replaces the base URI's fragment; otherwise, the base URI's fragment is preserved.
There are some differences between URIs created using this method and those created using the Uri(Uri, string) constructor:
AspectThis MethodUri Constructor
Path handlingAppends path segments with a separator if needed. If the relative path starts with '/', it replaces the base path.Uses URI resolution rules per RFC 3986, which can result in path segments being removed during normalization.
Query parametersPreserves and merges query parameters from both URIs.Query parameters in the relative URI completely replace those in the base URI.
Fragment handlingReplaces the fragment in the base URI with the fragment in the relative URI.Fragment in relative URI always replaces any fragment in the base URI.
Relative URI baseWorks with both relative and absolute base URIs.Throws an exception when the base URI is relative.
Empty relative URIReturns the base URI unchanged.May still perform normalization on the base URI.

See Also