Edit

Share via


Uri.TryCreate Method

Definition

Creates a new Uri. Does not throw an exception if the Uri cannot be created.

Overloads

TryCreate(Uri, Uri, Uri)

Creates a new Uri using the specified base and relative Uri instances.

TryCreate(Uri, String, Uri)

Creates a new Uri using the specified base and relative String instances.

TryCreate(String, UriKind, Uri)

Creates a new Uri using the specified String instance and a UriKind.

TryCreate(String, UriCreationOptions, Uri)

Creates a new Uri using the specified String instance and UriCreationOptions.

TryCreate(Uri, Uri, Uri)

Source:
UriExt.cs
Source:
UriExt.cs
Source:
UriExt.cs

Creates a new Uri using the specified base and relative Uri instances.

C#
public static bool TryCreate(Uri baseUri, Uri relativeUri, out Uri result);
C#
public static bool TryCreate(Uri? baseUri, Uri? relativeUri, out Uri? result);

Parameters

baseUri
Uri

The base URI.

relativeUri
Uri

The relative URI to add to the base Uri.

result
Uri

When this method returns, contains a Uri constructed from baseUri and relativeUri. This parameter is passed uninitialized.

Returns

true if the Uri was successfully created; otherwise, false.

Exceptions

baseUri is null.

Remarks

If this method returns true, the new Uri is in result.

This method constructs the URI, puts it in canonical form, and validates it. If an unhandled exception occurs, this method catches it. If you want to create a Uri and get exceptions use one of the Uri constructors.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TryCreate(Uri, String, Uri)

Source:
UriExt.cs
Source:
UriExt.cs
Source:
UriExt.cs

Creates a new Uri using the specified base and relative String instances.

C#
public static bool TryCreate(Uri baseUri, string relativeUri, out Uri result);
C#
public static bool TryCreate(Uri? baseUri, string? relativeUri, out Uri? result);

Parameters

baseUri
Uri

The base URI.

relativeUri
String

The string representation of the relative URI to add to the base Uri.

result
Uri

When this method returns, contains a Uri constructed from baseUri and relativeUri. This parameter is passed uninitialized.

Returns

true if the Uri was successfully created; otherwise, false.

Remarks

If this method returns true, the new Uri is in result.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TryCreate(String, UriKind, Uri)

Source:
UriExt.cs
Source:
UriExt.cs
Source:
UriExt.cs

Creates a new Uri using the specified String instance and a UriKind.

C#
public static bool TryCreate(string uriString, UriKind uriKind, out Uri result);
C#
public static bool TryCreate(string? uriString, UriKind uriKind, out Uri? result);

Parameters

uriString
String

The string representation of the Uri.

uriKind
UriKind

The type of the Uri.

result
Uri

When this method returns, contains the constructed Uri.

Returns

true if the Uri was successfully created; otherwise, false.

Remarks

If this method returns true, the new Uri is in result.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TryCreate(String, UriCreationOptions, Uri)

Source:
UriExt.cs
Source:
UriExt.cs
Source:
UriExt.cs

Creates a new Uri using the specified String instance and UriCreationOptions.

C#
public static bool TryCreate(string? uriString, in UriCreationOptions creationOptions, out Uri? result);

Parameters

uriString
String

The string representation of the Uri.

creationOptions
UriCreationOptions

Options that control how the Uri is created and behaves.

result
Uri

When this method returns, contains the constructed Uri.

Returns

true if the Uri was successfully created; otherwise, false.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9