Uri Uri Uri Uri Constructors

Definition

Overloads

Uri(String) Uri(String) Uri(String) Uri(String)

Initializes a new instance of the Uri class with the specified URI.

Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext)

Initializes a new instance of the Uri class from the specified instances of the SerializationInfo and StreamingContext classes.

Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean)

Initializes a new instance of the Uri class with the specified URI, with explicit control of character escaping.

Uri(String, UriKind) Uri(String, UriKind) Uri(String, UriKind)

Initializes a new instance of the Uri class with the specified URI. This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate.

Uri(Uri, String) Uri(Uri, String) Uri(Uri, String) Uri(Uri, String)

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

Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri)

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

Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean)

Initializes a new instance of the Uri class based on the specified base and relative URIs, with explicit control of character escaping.

Uri(String) Uri(String) Uri(String) Uri(String)

Initializes a new instance of the Uri class with the specified URI.

public:
 Uri(System::String ^ uriString);
public Uri (string uriString);
new Uri : string -> Uri
Public Sub New (uriString As String)

Parameters

uriString
String String String String

A string that identifies the resource to be represented by the Uri instance. Note that an IPv6 address in string form must be enclosed within brackets. For example, "http://[2607:f8b0:400d:c06::69]".

Exceptions

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

uriString is empty.

-or-

The scheme specified in uriString is not correctly formed. See CheckSchemeName(String).

-or-

uriString contains too many slashes.

-or-

The password specified in uriString is not valid.

-or-

The host name specified in uriString is not valid.

-or-

The file name specified in uriString is not valid.

-or-

The user name specified in uriString is not valid.

-or-

The host or authority name specified in uriString cannot be terminated by backslashes.

-or-

The port number specified in uriString is not valid or cannot be parsed.

-or-

The length of uriString exceeds 65519 characters.

-or-

The length of the scheme specified in uriString exceeds 1023 characters.

-or-

There is an invalid character sequence in uriString.

-or-

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

Examples

The following example creates a Uri instance with the URI "http://www.contoso.com/".

Uri^ baseUri = gcnew Uri( "http://www.contoso.com/" );
Uri myUri = new Uri("http://www.contoso.com/");

Dim myUri As New Uri("http://www.contoso.com/")

Remarks

This constructor creates a Uri instance from a URI string. It parses the URI, puts it in canonical format, and makes any required escape encodings.

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

This constructor assumes that the string parameter references an absolute URI and is equivalent to calling the Uri constructor with UriKind set to Absolute. If the string parameter passed to the constructor is a relative URI, this constructor will throw a UriFormatException.

Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext) Uri(SerializationInfo, StreamingContext)

Initializes a new instance of the Uri class from the specified instances of the SerializationInfo and StreamingContext classes.

protected:
 Uri(System::Runtime::Serialization::SerializationInfo ^ serializationInfo, System::Runtime::Serialization::StreamingContext streamingContext);
protected Uri (System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext);
new Uri : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> Uri

Parameters

serializationInfo
SerializationInfo SerializationInfo SerializationInfo SerializationInfo

An instance of the SerializationInfo class containing the information required to serialize the new Uri instance.

streamingContext
StreamingContext StreamingContext StreamingContext StreamingContext

An instance of the StreamingContext class containing the source of the serialized stream associated with the new Uri instance.

Exceptions

The serializationInfo parameter contains a null URI.

The serializationInfo parameter contains a URI that is empty.

-or-

The scheme specified is not correctly formed. See CheckSchemeName(String).

-or-

The URI contains too many slashes.

-or-

The password specified in the URI is not valid.

-or-

The host name specified in URI is not valid.

-or-

The file name specified in the URI is not valid.

-or-

The user name specified in the URI is not valid.

-or-

The host or authority name specified in the URI cannot be terminated by backslashes.

-or-

The port number specified in the URI is not valid or cannot be parsed.

-or-

The length of URI exceeds 65519 characters.

-or-

The length of the scheme specified in the URI exceeds 1023 characters.

-or-

There is an invalid character sequence in the URI.

-or-

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

Remarks

This constructor implements the ISerializable interface for the Uri class.

See also

Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean) Uri(String, Boolean)

Warning

This API is now obsolete.

Initializes a new instance of the Uri class with the specified URI, with explicit control of character escaping.

public:
 Uri(System::String ^ uriString, bool dontEscape);
[System.Obsolete("The constructor has been deprecated. Please use new Uri(string). The dontEscape parameter is deprecated and is always false. http://go.microsoft.com/fwlink/?linkid=14202")]
public Uri (string uriString, bool dontEscape);
new Uri : string * bool -> Uri
Public Sub New (uriString As String, dontEscape As Boolean)

Parameters

uriString
String String String String

A string that identifies the resource to be represented by the Uri instance. Note that an IPv6 address in string form must be enclosed within brackets. For example, "http://[2607:f8b0:400d:c06::69]".

dontEscape
Boolean Boolean Boolean Boolean

true if uriString is completely escaped; otherwise, false.

Exceptions

uriString is empty or contains only spaces.

-or-

The scheme specified in uriString is not valid.

-or-

uriString contains too many slashes.

-or-

The password specified in uriString is not valid.

-or-

The host name specified in uriString is not valid.

-or-

The file name specified in uriString is not valid.

-or-

The user name specified in uriString is not valid.

-or-

The host or authority name specified in uriString cannot be terminated by backslashes.

-or-

The port number specified in uriString is not valid or cannot be parsed.

-or-

The length of uriString exceeds 65519 characters.

-or-

The length of the scheme specified in uriString exceeds 1023 characters.

-or-

There is an invalid character sequence in uriString.

-or-

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

Examples

The following example creates a Uri instance for the URI http://www.contoso.com/Hello%20World.htm. Because the contained URI is completely escaped and is in canonical form, the dontEscape parameter can be set to true.

Uri^ myUri = gcnew Uri(  "http://www.contoso.com/Hello%20World.htm",true );
Uri myUri = new Uri("http://www.contoso.com/Hello%20World.htm", true);

Dim myUri As New Uri("http://www.contoso.com/Hello%20World.htm", True)

Remarks

This constructor creates a Uri instance from a URI string. It parses the URI and puts it in canonical format.

The dontEscape parameter controls whether reserved characters are translated into escape sequences. This parameter should be set to true only if you are certain that all reserved characters in the URI have been escaped. Setting the value to true for a URI that has not been completely escaped can cause unexpected behavior. It is strongly recommended that you always set this parameter to false.

If dontEscape is set to false, the constructor escapes any reserved characters by checking that all occurrences of percent (%) are followed by a valid escape sequence. If the character sequence following a percent is not valid, the percent is replaced by %25.

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

Uri(String, UriKind) Uri(String, UriKind) Uri(String, UriKind)

Initializes a new instance of the Uri class with the specified URI. This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate.

public:
 Uri(System::String ^ uriString, UriKind uriKind);
public Uri (string uriString, UriKind uriKind);
new Uri : string * UriKind -> Uri

Parameters

uriString
String String String String

A string that identifies the resource to be represented by the Uri instance. Note that an IPv6 address in string form must be enclosed within brackets. For example, "http://[2607:f8b0:400d:c06::69]".

uriKind
UriKind UriKind UriKind UriKind

Specifies whether the URI string is a relative URI, absolute URI, or is indeterminate.

Exceptions

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

uriString contains a relative URI and uriKind is Absolute.

or

uriString contains an absolute URI and uriKind is Relative.

or

uriString is empty.

-or-

The scheme specified in uriString is not correctly formed. See CheckSchemeName(String).

-or-

uriString contains too many slashes.

-or-

The password specified in uriString is not valid.

-or-

The host name specified in uriString is not valid.

-or-

The file name specified in uriString is not valid.

-or-

The user name specified in uriString is not valid.

-or-

The host or authority name specified in uriString cannot be terminated by backslashes.

-or-

The port number specified in uriString is not valid or cannot be parsed.

-or-

The length of uriString exceeds 65519 characters.

-or-

The length of the scheme specified in uriString exceeds 1023 characters.

-or-

There is an invalid character sequence in uriString.

-or-

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

Remarks

Relative and absolute URIs have different restrictions on their format. For example, a relative URI does not require a scheme or an authority. The value you specify in uriKind must match the type of URI passed in uriString. However, if RelativeOrAbsolute is specified, the URI string can be relative or absolute.

Uri(Uri, String) Uri(Uri, String) Uri(Uri, String) Uri(Uri, String)

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

public:
 Uri(Uri ^ baseUri, System::String ^ relativeUri);
public Uri (Uri baseUri, string relativeUri);
new Uri : Uri * string -> Uri
Public Sub New (baseUri As Uri, relativeUri As String)

Parameters

baseUri
Uri Uri Uri Uri

The base URI.

relativeUri
String String String String

The relative URI to add to the base URI.

Exceptions

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

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 1023 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:\\.

Examples

The following example creates a new instance of the Uri class by combining the relative URIs http://www.contoso.com and catalog/shownew.htm to form the absolute URI http://www.contoso.com/catalog/shownew.htm.

Uri^ baseUri = gcnew Uri(  "http://www.contoso.com" );
Uri^ myUri = gcnew Uri( baseUri, "catalog/shownew.htm" );
Console::WriteLine( myUri->ToString() );
Uri baseUri = new Uri("http://www.contoso.com");
 Uri myUri = new Uri(baseUri, "catalog/shownew.htm");

Console.WriteLine(myUri.ToString());
   
Dim baseUri As New Uri("http://www.contoso.com")
Dim myUri As New Uri(baseUri, "catalog/shownew.htm")

Console.WriteLine(myUri.ToString())

Remarks

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

If the baseUri has relative parts (like /api), then the relative part must be terminated with a slash, (like /api/), if the relative part of baseUri is to be preserved in the constructed Uri.

Additionally, if the relativeUri begins with a slash, then it will replace any relative part of the baseUri

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

Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri) Uri(Uri, Uri)

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

public:
 Uri(Uri ^ baseUri, Uri ^ relativeUri);
public Uri (Uri baseUri, Uri relativeUri);
new Uri : Uri * Uri -> Uri
Public Sub New (baseUri As Uri, relativeUri As Uri)

Parameters

baseUri
Uri Uri Uri Uri

An absolute Uri that is the base for the new Uri instance.

relativeUri
Uri Uri Uri Uri

A relative Uri instance that is combined with baseUri.

Exceptions

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, FormatException, instead.

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 1023 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:\\.

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.
String^ addressString1 = "http://www.contoso.com/";
String^ addressString2 = "catalog/shownew.htm?date=today";
Uri^ absoluteUri = gcnew Uri(addressString1);

// Create a relative Uri from a string.  allowRelative = true to allow for 
// creating a relative Uri.
Uri^ relativeUri = gcnew Uri(addressString2);

// Check whether the new Uri is absolute or relative.
if (  !relativeUri->IsAbsoluteUri )
   Console::WriteLine( "{0} is a relative Uri.", relativeUri );

// Create a new Uri from an absolute Uri and a relative Uri.
Uri^ combinedUri = gcnew Uri( absoluteUri,relativeUri );
Console::WriteLine( combinedUri->AbsoluteUri );
// Create an absolute Uri from a string.
Uri absoluteUri = new Uri("http://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)
    Console.WriteLine("{0} is a relative Uri.", relativeUri);

// Create a new Uri from an absolute Uri and a relative Uri.
Uri combinedUri = new Uri(absoluteUri, relativeUri);
Console.WriteLine(combinedUri.AbsoluteUri);
    ' Create an absolute Uri from a string.
    Dim absoluteUri As New Uri("http://www.contoso.com/")
    
    ' Create a relative Uri from a string.  allowRelative = true to allow for 
    ' creating a relative Uri.
    Dim relativeUri As New Uri("/catalog/shownew.htm?date=today")
    
    ' Check whether the new Uri is absolute or relative.
    If Not relativeUri.IsAbsoluteUri Then
        Console.WriteLine("{0} is a relative Uri.", relativeUri)
    End If 
    ' Create a new Uri from an absolute Uri and a relative Uri.
    Dim combinedUri As New Uri(absoluteUri, relativeUri)
    Console.WriteLine(combinedUri.AbsoluteUri)

End Sub 'SampleConstructor

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.

If the baseUri has relative parts (like /api), then the relative part must be terminated with a slash, (like /api/), if the relative part of baseUri is to be preserved in the constructed Uri.

Additionally, if the relativeUri begins with a slash, then it will replace any relative part of the baseUri

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

Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean) Uri(Uri, String, Boolean)

Warning

This API is now obsolete.

Initializes a new instance of the Uri class based on the specified base and relative URIs, with explicit control of character escaping.

public:
 Uri(Uri ^ baseUri, System::String ^ relativeUri, bool dontEscape);
[System.Obsolete("The constructor has been deprecated. Please new Uri(Uri, string). The dontEscape parameter is deprecated and is always false. http://go.microsoft.com/fwlink/?linkid=14202")]
public Uri (Uri baseUri, string relativeUri, bool dontEscape);
new Uri : Uri * string * bool -> Uri
Public Sub New (baseUri As Uri, relativeUri As String, dontEscape As Boolean)

Parameters

baseUri
Uri Uri Uri Uri

The base URI.

relativeUri
String String String String

The relative URI to add to the base URI.

dontEscape
Boolean Boolean Boolean Boolean

true if uriString is completely escaped; otherwise, false.

Exceptions

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 1023 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:\\.

Examples

The following example creates a new instance of the Uri class by combining the relative URIs http://www.contoso.com and Hello%20World.htm to form an absolute URI.

Uri^ baseUri = gcnew Uri( "http://www.contoso.com/" );
Uri^ myUri = gcnew Uri( baseUri,"Hello%20World.htm",false );
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "Hello%20World.htm",false);

Dim baseUri As New Uri("http://www.contoso.com")
Dim myUri As New Uri(baseUri, "Hello%20World.htm", False)

Remarks

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

The dontEscape parameter controls whether reserved characters are translated into escape sequences. This parameter should be set to true only if you are certain that all reserved characters in the URI have been escaped. Setting the value to true for a URI that has not been completely escaped can cause unexpected behavior. It is strongly recommended that you always set this parameter to false. If dontEscape is set to false, the constructor escapes any reserved characters by checking that all occurrences of percent (%) are followed by a valid escape sequence. If the character sequence following a percent is not valid, the percent is replaced by %25.

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

Notes to Callers

Because of security concerns, your application should not call this constructor with URI strings from untrusted sources and with dontEscape set to true. Alternately, you can check a URI string for validity by calling the IsWellFormedOriginalString() method prior to calling this constructor.

Applies to