Table of Contents

Class Uri

Namespace
System
Assembly
System.Net.Http.dll

Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.

public class Uri
Inheritance
Uri
Inherited Members
Extension Methods

Constructors

Uri(string)

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

public Uri(string uriString)

Parameters

uriString string

A URI.

Remarks

This constructor parses the URI string, therefore it can be used to validate a URI.

Exceptions

ArgumentNullException

The uriString is null.

ArgumentException

The uriString is empty.

-or-

The scheme specified in uriString is not correctly formed.

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

Uri(string, UriKind)

Constructs Uri from string and enumeration that tell what is the type of Uri.

public Uri(string uriString, UriKind kind)

Parameters

uriString string

String to construct Uri from

kind UriKind

Type of Uri to construct

Exceptions

ArgumentException

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

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, string relativeUri = null)

Parameters

baseUri Uri

The base URI.

relativeUri string

The relative URI to add to the base URI.

Exceptions

ArgumentNullException

baseUri is null.

ArgumentOutOfRangeException

baseUri is not an absolute Uri instance.

FormatException

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

Fields

HttpDefaultPort

Default port for http protocol - 80

public const int HttpDefaultPort = 80

Field Value

int

HttpsDefaultPort

Default port for https protocol - 443

public const int HttpsDefaultPort = 443

Field Value

int

UnknownPort

Constant to indicate that port for this protocol is unknown

protected const int UnknownPort = -1

Field Value

int

UriSchemeHttp

Specifies that the URI is accessed through the Hypertext Transfer Protocol (HTTP). This field is read-only.

public const string UriSchemeHttp = "http"

Field Value

string

UriSchemeHttps

Specifies that the URI is accessed through the Secure Hypertext Transfer Protocol (HTTPS). This field is read-only.

public const string UriSchemeHttps = "https"

Field Value

string

_AbsolutePath

Member variable that keeps absolute path.

protected string _AbsolutePath

Field Value

string

_Flags

Member variable that keeps internal flags/

protected Uri.Flags _Flags

Field Value

Uri.Flags

_OriginalUriString

Member variable that keeps original string passed to Uri constructor.

protected string _OriginalUriString

Field Value

string

_absoluteUri

Member variable that keeps absolute uri (generated in method ParseUriString)

protected string _absoluteUri

Field Value

string

_host

Member variable that keeps host name ( http and https ).

protected string _host

Field Value

string

_hostNameType

Type of the host.

protected UriHostNameType _hostNameType

Field Value

UriHostNameType

_isAbsoluteUri

Member variable that keeps boolean if Uri is absolute.

protected bool _isAbsoluteUri

Field Value

bool

_isUnc

Member variable that tells if path is UNC ( Universal Naming Convention ) In this class it is always false, but can be changed in derived classes.

protected bool _isUnc

Field Value

bool

_port

Member variable that keeps port used by this uri.

protected int _port

Field Value

int

_scheme

Member variable that keeps scheme of Uri.

protected string _scheme

Field Value

string

Properties

AbsolutePath

Gets a local operating-system representation of a file name.

public string AbsolutePath { get; }

Property Value

string

A String containing the local operating-system representation of a file name.

Exceptions

InvalidOperationException

This instance represents a relative URI, and this property is valid only for absolute URIs.

AbsoluteUri

Gets a string containing the absolute uri or entire uri of this instance.

public string AbsoluteUri { get; }

Property Value

string

A String containing the entire URI.

Host

Gets the host component of this instance.

public string Host { get; }

Property Value

string

A String containing the host name. This is usually the DNS host name or IP address of the server.

HostNameType

Gets the type of the host name specified in the URI.

public UriHostNameType HostNameType { get; }

Property Value

UriHostNameType

A member of the UriHostNameType enumeration.

IsAbsoluteUri

Gets whether the Uri instance is absolute.

public bool IsAbsoluteUri { get; }

Property Value

bool

true if the Uri instance is absolute; otherwise, false.

IsLoopback

Gets whether the specified Uri refers to the local host.

public bool IsLoopback { get; }

Property Value

bool

true if the host specified in the Uri is the local computer; otherwise, false.

IsUnc

Gets whether the specified Uri is a universal naming convention (UNC) path.

public bool IsUnc { get; }

Property Value

bool

true if the Uri is a UNC path; otherwise, false.

Exceptions

InvalidOperationException

This instance represents a relative URI, and this property is valid only for absolute URIs.

OriginalString

Gets the original URI string that was passed to the Uri constructor.

public string OriginalString { get; }

Property Value

string

Port

Gets the port number of this URI.

public int Port { get; }

Property Value

int

An Int32 value containing the port number for this URI.

Exceptions

InvalidOperationException

This instance represents a relative URI, and this property is valid only for absolute URIs.

Scheme

Gets the scheme name for this URI.

public string Scheme { get; }

Property Value

string

A String containing the scheme for this URI, converted to lowercase.

Exceptions

InvalidOperationException

This instance represents a relative URI, and this property is valid only for absolute URIs.

Methods

ConstructAbsoluteUri(string)

Constructs an absolute Uri from a URI string.

protected bool ConstructAbsoluteUri(string uriString)

Parameters

uriString string

A URI.

Returns

bool

Remarks

Equals(object)

Determines whether the specified object is equal to the current object.

public override bool Equals(object o)

Parameters

o object

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

IsIPv4(string)

Returns if host name is IP adress 4 bytes. Like 192.1.1.1

protected bool IsIPv4(string host)

Parameters

host string

string with host name

Returns

bool

True if name is string with IPv4 address

IsWellFormedUriString(string, UriKind)

Indicates whether the string is well-formed by attempting to construct a URI with the string.

public static bool IsWellFormedUriString(string uriString, UriKind uriKind)

Parameters

uriString string

A URI.

uriKind UriKind

The type of the URI in uriString.

Returns

bool

true if the string was well-formed in accordance with RFC 2396 and RFC 2732; otherwise false.

ParseSchemeSpecificPart(string, int)

Parse Scheme-specific part of uri for host, port and absolute path Briefed syntax abstracted from .NET FX: Group 1 - http, https, ftp, file, gopher, nntp, telnet, ldap, net.tcp and net.pipe Must be rooted. The 1st segment is authority. Empty path should be replace as '/'

Group 2 - file Reminder: Treat all '' as '/' If it starts with only one '/', host should be empty Otherwise, all leading '/' should be ignored before searching for 1st segment. The 1st segment is host

Group 3 - news and uuid Authority always be empty. Everything goes to path.

Group 4 - mailto and all other shemes The 1st segment is authority iff it was not rooted.

Group 5 - all other schemes The 1st segment is authority iff it was rooted. Empty path should be replace as '/'

protected bool ParseSchemeSpecificPart(string sUri, int iStart)

Parameters

sUri string

Scheme-specific part of uri

iStart int

Returns

bool

Split(string, int, out string, out string, bool)

protected void Split(string sUri, int iStart, out string sAuthority, out string sPath, bool bReplaceEmptyPath)

Parameters

sUri string
iStart int
sAuthority string
sPath string
bReplaceEmptyPath bool

TryParseUriString(string)

Internal method parses a URI string into Uri variables

protected bool TryParseUriString(string uriString)

Parameters

uriString string

A Uri.

Returns

bool

Exceptions

ArgumentNullException

The uriString is null.

Exception

See constructor description.

ValidateUriPart(string, int)

Validates that part of Uri after sheme is valid for unknown Uri scheme

protected bool ValidateUriPart(string uriString, int startIndex)

Parameters

uriString string

Uri string

startIndex int

Index in the string where Uri part ( after scheme ) starts

Returns

bool

Operators

operator ==(Uri, Uri)

public static bool operator ==(Uri lhs, Uri rhs)

Parameters

lhs Uri
rhs Uri

Returns

bool

operator !=(Uri, Uri)

public static bool operator !=(Uri lhs, Uri rhs)

Parameters

lhs Uri
rhs Uri

Returns

bool