Edit

Share via


Uri.GetLeftPart(UriPartial) Method

Definition

Gets the specified portion of a Uri instance.

C#
public string GetLeftPart(UriPartial part);

Parameters

part
UriPartial

One of the enumeration values that specifies the end of the URI portion to return.

Returns

The specified portion of the Uri instance.

Exceptions

The current Uri instance is not an absolute instance.

The specified part is not valid.

Examples

The following example creates a Uri instance and writes the path to the console.

C#
// Create Uri
Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search");
Console.WriteLine(uriAddress.Fragment);
Console.WriteLine("Uri {0} the default port ", uriAddress.IsDefaultPort ? "uses" : "does not use");

Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Path));
Console.WriteLine("Hash code {0}", uriAddress.GetHashCode());
// The example displays output similar to the following:
//        #search
//        Uri uses the default port
//        The path of this Uri is http://www.contoso.com/index.htm
//        Hash code -988419291

Remarks

The GetLeftPart method returns a string containing the leftmost portion of the URI string, ending with the portion specified by part.

GetLeftPart includes delimiters in the following cases:

  • Scheme includes the scheme delimiter.
  • Authority does not include the path delimiter.
  • Path includes the path delimiter and any delimiters in the original URI up to the query or fragment delimiter.
  • Query includes the Path, plus the query and its delimiter.

The following examples show a URI and the results of calling GetLeftPart with Scheme, Authority, Path, or Query.

URI Scheme Authority Path Query
http://www.contoso.com/index.htm?date=today http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm?date=today
http://www.contoso.com/index.htm#main http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm
mailto:user@contoso.com?subject=uri mailto: <none> mailto:user@contoso.com mailto:user@contoso.com?subject=uri
nntp://news.contoso.com/123456@contoso.com nntp:// nntp://news.contoso.com nntp://news.contoso.com/123456@contoso.com nntp://news.contoso.com/123456@contoso.com
news:123456@contoso.com news: <none> news:123456@contoso.com news:123456@contoso.com
file://server/filename.ext file:// file://server file://server/filename.ext file://server/filename.ext

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 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 2.0, 2.1