Partager via


Uri Constructor (Uri, Uri)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the Uri class based on the combination of a specified base Uri instance and a relative Uri instance.

Namespace:  System
Assembly:  System (in System.dll)

Syntax

'Declaration
Public Sub New ( _
    baseUri As Uri, _
    relativeUri As Uri _
)
public Uri(
    Uri baseUri,
    Uri relativeUri
)

Parameters

  • baseUri
    Type: System.Uri
    An absolute Uri that is the base for the new Uri instance.
  • relativeUri
    Type: System.Uri
    A relative Uri instance that is combined with baseUri.

Exceptions

Exception Condition
ArgumentNullException

baseUri is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

baseUri is not an absolute Uri instance.

UriFormatException

The URI formed by combining baseUri and relativeUri is empty or contains only spaces.

-or-

The scheme specified in the URI formed by combining baseUri and relativeUri is not valid.

-or-

The URI formed by combining baseUri and relativeUri contains too many slashes.

-or-

The password specified in the URI formed by combining baseUri and relativeUri is not valid.

-or-

The host name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or-

The file name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or-

The user name specified in the URI formed by combining baseUri and relativeUri is not valid.

-or-

The host or authority name specified in the URI formed by combining baseUri and relativeUri cannot be terminated by backslashes.

-or-

The port number specified in the URI formed by combining baseUri and relativeUri is not valid or cannot be parsed.

-or-

The length of the URI formed by combining baseUri and relativeUri exceeds 65519 characters.

-or-

The length of the scheme specified in the URI formed by combining baseUri and relativeUri exceeds 1024 characters.

-or-

There is an invalid character sequence in the URI formed by combining baseUri and relativeUri.

-or-

The MS-DOS path specified in uriString must start with c:\\.

Remarks

This constructor creates a new Uri instance by combining an absolute Uri instance, baseUri, with a relative Uri instance, relativeUri. If relativeUri is an absolute Uri instance (containing a scheme, host name, and optionally a port number), the Uri instance is created using only relativeUri.

This constructor allows a Silverlight application to create a Uri instance for many schemes as described in the Scheme property. However, only Uri instances for the UriSchemeHttp or UriSchemeHttps schemes are supported by the WebClient and HTTP classes in the System.Net namespace.

This constructor does not ensure that the Uri refers to an accessible resource.

Examples

This example creates an absolute Uri instance, absoluteUri, and a relative Uri instance, relativeUri. A new Uri instance, combinedUri, is then created from these two instances.

' Create an absolute Uri from a string.
Dim absoluteUri As Uri = New Uri("https://www.contoso.com/")

' Create a relative Uri from a string.  allowRelative = true to allow for 
' creating a relative Uri.
Dim relativeUri As Uri = new Uri("/catalog/shownew.htm?date=today", UriKind.Relative)
' Check whether the new Uri is absolute or relative.
If relativeUri.IsAbsoluteUri Then 
  outputBlock.Text &= relativeUri.ToString()
  outputBlock.Text &= " is an absolute Uri."
  outputBlock.Text &= vbCrLf
Else
  outputBlock.Text &= relativeUri.ToString()
  outputBlock.Text &= " is a relative Uri."
  outputBlock.Text &= vbCrLf
End If

' Create a new Uri from an absolute Uri and a relative Uri.
Dim combinedUri As Uri = New Uri(absoluteUri, relativeUri)
outputBlock.Text &= combinedUri.AbsoluteUri.ToString()
outputBlock.Text &= vbCrLf
// Create an absolute Uri from a string.
Uri absoluteUri = new Uri("https://www.contoso.com/");

// Create a relative Uri from a string.  allowRelative = true to allow for 
// creating a relative Uri.
Uri relativeUri = new Uri("/catalog/shownew.htm?date=today", UriKind.Relative);

// Check whether the new Uri is absolute or relative.
if (!relativeUri.IsAbsoluteUri) 
{
   outputBlock.Text += relativeUri;
   outputBlock.Text += " is a relative Uri.\n";
}

// Create a new Uri from an absolute Uri and a relative Uri.
Uri combinedUri = new Uri(absoluteUri, relativeUri);
outputBlock.Text += combinedUri.AbsoluteUri;
outputBlock.Text += "\n";

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.