Constructs and initializes a new instance of the Uri class by combining the specified base and relative URIs.
- baseUri
- A Uri containing the base URI. This parameter can, but is not required to contain a terminating slash ("/") character.
- relativeUri
- A string containing the relative URI to add to the base URI. This parameter can, but is not required to contain a leading slash ("/") character.
- dontEscape
- true if baseUri and relativeUri are already escaped; otherwise, false.
Type Reason UriFormatException relativeUri is in an invalid form. NullReferenceException baseUri is null.
This constructor compensates for the presence or absence of a terminating slash in baseUri and/or a leading slash in relativeUri to produce a well-formed URI.
If the relative URI contains a Uri.Scheme that is the same as the scheme of the base URI and the Uri.SchemeDelimiter is not present, or the relative URI does not contain a scheme, the new instance is composed of the relative URI (without its scheme component, if any) qualified by the scheme and authority information from the base URI.
If the relative URI contains a Uri.Scheme followed by the Uri.SchemeDelimiter, it is treated as an absolute URI and the base URI is ignored. If the relative URI contains a scheme that differs from the scheme of the base URI, the base URI is ignored. If the Uri.SchemeDelimiter is not present in the relative URI, it is assumed, and the new instance is constructed as though the relative URI were an absolute URI.
Note: When the base URI is ignored, only the components of the relative URI are used to construct the new instance.
The following example creates new instances of the Uri class by combining a Uri instance representing the base URI and a string containing a relative URI.
C# Example
using System; public class UriTest { public static void Main() { // Typical base and relative URI constructor usage. Uri baseUri = new Uri("http://www.contoso.com", true); Uri myUri = new Uri(baseUri, "index.htm",true); Console.WriteLine("Typical usage: {0}",myUri.ToString()); // Base and relative URI contain slashes. Uri baseUri2 = new Uri("http://www.contoso.com/", true); Uri myUri2 = new Uri(baseUri2, "/index.htm",true); Console.WriteLine("Slash example: {0}",myUri2.ToString()); // Relative URI contains a different scheme than the base URI. Uri baseUri3 = new Uri("http://www.contoso.com/", true); Uri myUri3 = new Uri(baseUri3, "ftp://www.contoso2.com/index.htm",true); Console.WriteLine("Different schemes: {0}", myUri3.ToString()); // Relative URI contains the same scheme as the base URI. // The scheme delimiter is not present in the relative URI. Uri baseUri4 = new Uri("http://www.contoso.com/", true); Uri myUri4 = new Uri(baseUri4, "http:www.contoso2.com/index.htm",true); Console.WriteLine("Same schemes - relative treated as relative: {0}",myUri4.ToString()); // Relative URI contains the same scheme as the base URI. // The scheme delimiter is present in the relative URI. Uri baseUri5 = new Uri("http://www.contoso.com/", true); Uri myUri5 = new Uri(baseUri5, "http://www.contoso2/index.htm",true); Console.WriteLine("Same schemes - relative treated as absolute: {0}",myUri5.ToString()); } }The output is
Typical usage: http://www.contoso.com/index.htm
Slash example: http://www.contoso.com/index.htm
Different schemes: ftp://www.contoso2.com/index.htm
Same schemes - relative treated as relative: http://www.contoso.com/www.contoso2 .com/index.htm
Same schemes - relative treated as absolute: http://www.contoso2/index.htm
Namespace: System
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0