public class Uri
Object
Uri
System
Networking
Provides an object representation of a uniform resource identifier (URI) as defined by IETF RFC 2396.
[Note: A Uniform Resource Identifier (URI) is a compact string of characters used to identify a resource located on a computer. A resource can be anything that has identity. Examples of resources that might be accessed using a URI include an electronic document, an image, a web service, and a collection of other resources. A URI is represented as a sequence of characters. While the exact format of a URI is determined by the protocol used to access the resource, many URI consist of four major components:<scheme>://<authority><path>?<query>
Scheme - Indicates a protocol used to access the resource.
Authority - Indicates the naming authority (server or registry) that governs the namespace defined by the remainder of the URI. The authority component is composed of userinfo, host and port subcomponents in the form <userinfo>@<host>:<port>. Only the host subcomponent is required to be present in the Authority component. Authority information is stored in the System.Uri.Authority property.
Path - Identifies the resource within the scope of the scheme and, if present, the authority. This information is stored in the System.Uri.AbsolutePath , System.Uri.PathAndQuery, and System.Uri.LocalPath properties.
Query - Parameter information that is passed to the executable script identified by the URI. The query, if present, is the last element in a URI and begins with a "?". This information is stored in the System.Uri.Query property.
Userinfo - [Subcomponent of Authority] Consists of a user name and, optionally, scheme-specific authorization information used to access Host . The userinfo, if present, is separated from the Host component by the "@" character. Note that for some URI schemes, the format of the userinfo subcomponent is "username:password". Passing authorization information in this manner is strongly discouraged due to security issues. The userinfo information is stored in the System.Uri.UserInfo property.
Host - [Subcomponent of Authority] The Domain Name system (DNS) name or IP4 address of a machine that provides access to the resource. This information is stored in the System.Uri.Host property.
Port - [Subcomponent of Authority ] The network port number used to connect to the host. If no port number is specified in the URI, most schemes designate protocols that have a default port number. This information is stored in the System.Uri.Port property.
Fragment - The fragment is not part of the URI, but is used in conjunction with the URI and is included here for completeness. This component contains resource-specific information that is used after a resource is retrieved. The fragment , if present, is separated from the URI by the "#" character. This information is stored in the System.Uri.Fragment property.
URIs include components consisting of or delimited by certain special (reserved) characters that have a special meaning in a URI component. If the reserved meaning is not intended, then the character is required to be escaped in the URI. An escaped character is encoded as a character triplet consisting of the percent character "%" followed by the US-ASCII character code specified as two hexadecimal digits. For example, "%20" is the escaped encoding for the US-ASCII space character. The URI represented by a Uri instance is always in "escaped" form. The following characters are reserved:
To transform the URI contained in a Uri instance from an escape encoded URI to a human-readable URI, use the System.Uri.ToString method.
- Semi-colon (";" )
- Forward slash ( "/")
- Question mark ( "?" )
- Colon ( ":" )
- At-sign ("@")
- Ampersand ( "&" )
- Equal sign ("=" )
- Plus sign ("+" )
- US Dollar sign ("$" )
- Comma (",")
]
URIs are stored as canonical URIs in escaped encoding, with all characters with ASCII values greater than 127 replaced with their hexadecimal equivalents. The Uri constructors do not escape URI strings if the string is a well-formed URI, including a scheme identifier, that contains escape sequences. To put the URI in canonical form, the Uri constructors perform the following steps.
The Uri class stores only absolute URIs (for example, "http://www.contoso.com/index.htm"). Relative URIs (for example, "/new/index.htm") are expanded to absolute form using a specified base URI. The System.Uri.MakeRelative(System.Uri) method converts absolute URIs to relative URIs.
- Converts the URI scheme to lowercase.
- Converts the host name to lowercase.
- Removes default and empty port numbers.
- Simplifies the URI by removing superfluous segments such as "/" and "/test" segments.
The Uri class properties are read-only; to modify a Uri instance use the UriBuilder class.
System Namespace
Uri Constructors
Uri(System.String) Constructor
Uri(System.String, bool) Constructor
Uri(System.Uri, System.String) Constructor
Uri(System.Uri, System.String, bool) Constructor
Uri Methods
Uri.Canonicalize Method
Uri.CheckHostName Method
Uri.CheckSchemeName Method
Uri.CheckSecurity Method
Uri.Equals Method
Uri.Escape Method
Uri.EscapeString Method
Uri.FromHex Method
Uri.GetHashCode Method
Uri.GetLeftPart Method
Uri.HexEscape Method
Uri.HexUnescape Method
Uri.IsBadFileSystemCharacter Method
Uri.IsExcludedCharacter Method
Uri.IsHexDigit Method
Uri.IsHexEncoding Method
Uri.IsReservedCharacter Method
Uri.MakeRelative Method
Uri.Parse Method
Uri.ToString Method
Uri.Unescape Method
Uri Fields
Uri.SchemeDelimiter Field
Uri.UriSchemeFile Field
Uri.UriSchemeFtp Field
Uri.UriSchemeGopher Field
Uri.UriSchemeHttp Field
Uri.UriSchemeHttps Field
Uri.UriSchemeMailto Field
Uri.UriSchemeNews Field
Uri.UriSchemeNntp Field
Uri Properties
Uri.AbsolutePath Property
Uri.AbsoluteUri Property
Uri.Authority Property
Uri.Fragment Property
Uri.Host Property
Uri.HostNameType Property
Uri.IsDefaultPort Property
Uri.IsFile Property
Uri.IsLoopback Property
Uri.LocalPath Property
Uri.PathAndQuery Property
Uri.Port Property
Uri.Query Property
Uri.Scheme Property
Uri.UserEscaped Property
Uri.UserInfo Property
public Uri(string uriString);
Constructs and initializes a new instance of the Uri class by parsing the specified URI.
- uriString
- A String containing a URI.
Exception Type Condition ArgumentNullException uriString is null
.
UriFormatException uriString is a zero length string or contains only spaces. -or-
uriString is in an invalid form and cannot be parsed.
This constructor is equivalent to calling the Uri (String, Boolean) constructor, and specifying uriString andfalse
as the arguments.
System.Uri Class, System Namespace
public Uri(string uriString, bool dontEscape);
Constructs and initializes a new instance of the Uri class by parsing the specified URI.
- uriString
- A String containing a URI.
- dontEscape
true
if the URI in uriString is already escaped; otherwise,false
.
Exception Type Condition ArgumentNullException uriString is null
.
UriFormatException uriString is a zero length string or contains only spaces. -or-
The parsing routine detected a scheme in an invalid form.
-or-
The parser detected more than two consecutive slashes in a URI that does not use the "file" scheme.
-or-
uriString is in an invalid form and cannot be parsed.
This constructor parses the URI, places its components into the appropriate properties, and puts the URI in canonical form. If the specified URI does not contain a scheme component, the URI is parsed using "file" as the scheme.
The following example creates a Uri instance for the URI "http://www.contoso.com/Hello%20World.htm". Because the URI contains escaped characters, the third parameter, dontEscape , is set totrue
.
using System; public class UriTest { public static void Main() { Uri myUri = new Uri("http://www.contoso.com/Hello%20World.htm", true); Console.WriteLine(myUri.ToString()); } }The output is
http://www.contoso.com/Hello World.htm
System.Uri Class, System Namespace
public Uri(Uri baseUri, string relativeUri);
Constructs and initializes a new instance of the Uri class by combining the specified base and relative URIs.
- baseUri
- A Uri containing a base URI.
- relativeUri
- A String containing a relative URI.
Exception Type Condition UriFormatException relativeUri is in an invalid form.
NullReferenceException baseUri is null
.
This constructor is equivalent to calling the Uri (Uri, String, Boolean) constructor, and specifying baseUri, relativeUri, andfalse
as the arguments.
System.Uri Class, System Namespace
public Uri(Uri baseUri, string relativeUri, bool dontEscape);
Constructs and initializes a new instance of the Uri class by combining the specified base and relative URIs.
- baseUri
- A Uri containing the base URI. This parameter can, but is not required to contain a terminating slash ("/") character.
- relativeUri
- A String containing the relative URI to add to the base URI. This parameter can, but is not required to contain a leading slash ("/") character.
- dontEscape
true
if baseUri and relativeUri are already escaped; otherwise,false
.
Exception Type Condition UriFormatException relativeUri is in an invalid form.
NullReferenceException baseUri is null
.
This constructor compensates for the presence or absence of a terminating slash in baseUri and/or a leading slash in relativeUri to produce a well-formed URI.If the relative URI contains a System.Uri.Scheme that is the same as the scheme of the base URI and the System.Uri.SchemeDelimiter is not present, or the relative URI does not contain a scheme, the new instance is composed of the relative URI (without its scheme component, if any) qualified by the scheme and authority information from the base URI.
If the relative URI contains a System.Uri.Scheme followed by the System.Uri.SchemeDelimiter, it is treated as an absolute URI and the base URI is ignored. If the relative URI contains a scheme that differs from the scheme of the base URI, the base URI is ignored. If the System.Uri.SchemeDelimiter is not present in the relative URI, it is assumed, and the new instance is constructed as though the relative URI were an absolute URI.
[Note: When the base URI is ignored, only the components of the relative URI are used to construct the new instance.]
The following example creates new instances of the Uri class by combining a Uri instance representing the base URI and a string containing a relative URI.
using System; public class UriTest { public static void Main() { // Typical base and relative URI constructor usage. Uri baseUri = new Uri("http://www.contoso.com", true); Uri myUri = new Uri(baseUri, "index.htm",true); Console.WriteLine("Typical usage: {0}",myUri.ToString()); // Base and relative URI contain slashes. Uri baseUri2 = new Uri("http://www.contoso.com/", true); Uri myUri2 = new Uri(baseUri2, "/index.htm",true); Console.WriteLine("Slash example: {0}",myUri2.ToString()); // Relative URI contains a different scheme than the base URI. Uri baseUri3 = new Uri("http://www.contoso.com/", true); Uri myUri3 = new Uri(baseUri3, "ftp://www.contoso2.com/index.htm",true); Console.WriteLine("Different schemes: {0}", myUri3.ToString()); // Relative URI contains the same scheme as the base URI. // The scheme delimiter is not present in the relative URI. Uri baseUri4 = new Uri("http://www.contoso.com/", true); Uri myUri4 = new Uri(baseUri4, "http:www.contoso2.com/index.htm",true); Console.WriteLine("Same schemes - relative treated as relative: {0}",myUri4.ToString()); // Relative URI contains the same scheme as the base URI. // The scheme delimiter is present in the relative URI. Uri baseUri5 = new Uri("http://www.contoso.com/", true); Uri myUri5 = new Uri(baseUri5, "http://www.contoso2/index.htm",true); Console.WriteLine("Same schemes - relative treated as absolute: {0}",myUri5.ToString()); } }The output is
Typical usage: http://www.contoso.com/index.htm
Slash example: http://www.contoso.com/index.htm
Different schemes: ftp://www.contoso2.com/index.htm
Same schemes - relative treated as relative: http://www.contoso.com/www.contoso2 .com/index.htm
Same schemes - relative treated as absolute: http://www.contoso2/index.htm
System.Uri Class, System Namespace
protected virtual void Canonicalize();
Converts the components of the URI represented by the current instance to canonical form.
[Behaviors: This method converts the URI to a format suitable for machine interpretation according to the scheme of the current instance. The conversions are required to preserve all information that could, if removed or altered, change the URI represented by the current instance. ]
[Default: This method performs the following conversions:
]
- Converts file references to the format of the current platform, for example on a Windows system, file://c|/AFile.txt is converted to "file:///c:/AFile.txt".
- Converts any backslash characters ('\') to forward slashes ('/').
- Compresses multiple consecutive forward slashes ('/') in the path component to a single forward slash.
- Compresses any path meta sequences ("/." and "/..").
[Overrides: Override this method to canonicalize the type derived from Uri .]
[Usage: Applications do not call this method; it is called by constructors after parsing the URI and escaping the components. ]
System.Uri Class, System Namespace
public static UriHostNameType CheckHostName(string name);
Returns a value that describes the format of a host name string.
- name
- A String containing the host name to validate.
A UriHostNameType that indicates the type of the host name. If the type of the host name cannot be determined, or the host name isnull
or a zero-length string, returns System.UriHostNameType.Unknown .
The following example demonstrates using the System.Uri.CheckHostName(System.String) method.
using System; public class UriTest { public static void Main() { Console.WriteLine(Uri.CheckHostName("www.contoso.com")); } }The output is
Dns
System.Uri Class, System Namespace
public static bool CheckSchemeName(string schemeName);
Returns a Boolean value indicating whether the specified scheme name is valid.
- schemeName
- A String containing the scheme name to validate.
true
if the scheme name is valid; otherwise,false
. If schemeName isnull
or is a zero-length string, returnsfalse
.
[Note: The scheme name is required to begin with a letter, and contain only letters, digits, and the characters '.', '+' or '-'.]
System.Uri Class, System Namespace
protected virtual void CheckSecurity();
Checks the current instance for character sequences that can result in unauthorized access to resources, and removes them.
[Behaviors: This method checks for invalid or dangerous character sequences in the components of the current instance, and removes them. The semantics that determine whether a character sequence presents a security risk are determined by the scheme of the current instance.]
[Default: The default implementation does nothing.]
[Overrides: Override this method to provide security checks for types derived from Uri.]
[Usage: Invoke this method on instances of types derived from Uri to remove any URI content that allows unauthorized access to resources.]
System.Uri Class, System Namespace
public override bool Equals(object comparand);
Compares the current instance and the specified object for equality.
- comparand
- The Uri instance to compare with the current instance. This argument can be a String or a Uri .
true
if comparand represents the same URI (ignoring any fragment or query information) as the current instance; otherwise,false
. If comparand isnull
, a zero-length string, or is not an instance of String or Uri , returns false.
If comparand is a String, it is converted to a Uri by calling Uri(comparand).The System.Uri.Scheme, System.Uri.Host and unescaped version of the System.Uri.AbsolutePath of the current instance and comparand are compared for equality.
If the scheme of the current instance is the System.Uri.UriSchemeFile scheme, the absolute paths are compared in accordance with the case sensitivity of the current platform.
[Note: This method overrides System.Object.Equals(System.Object).]
System.Uri Class, System Namespace
protected virtual void Escape();
Converts any unsafe or reserved characters in the System.Uri.AbsolutePath component to equivalent escaped hexadecimal sequences.
[Behaviors: Converts any unsafe or reserved characters in the System.Uri.AbsolutePath component to a character sequence consisting of a "%" followed by the hexadecimal value of the character as described by IETF 2396.If the path component of the current instance is
null
, the escaped path is System.String.Empty.]
[Default: As described above.]
[Overrides: Override this method to customize the escaping behavior provided by the Uri type.]
[Usage: Applications typically do not call this method; it is intended for use by the constructors.]
[Note: For additional information on escaping URI, see section 2 of RFC 2396.]
System.Uri Class, System Namespace
protected static string EscapeString(string str);
Converts a string to its escaped representation.
- str
- A String to convert to its escaped representation.
A String containing the escaped representation of str .
The string is escaped in accordance with RFC 2396.
System.Uri Class, System Namespace
public static int FromHex(char digit);
Returns the decimal value of a hexadecimal digit.
- digit
- The hexadecimal digit (0-9, a-f, A-F) to convert.
A Int32 containing an integer from 0 - 15 that corresponds to the specified hexadecimal digit.
Exception Type Condition ArgumentException digit is not a valid hexadecimal digit (0-9, a-f, A-F).
System.Uri Class, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing the hash code for this instance.
The hash code is generated without the fragment component. For example, the URIs "http://www.contoso.com/index.htm#search" and "http://www.contoso.com/index.htm" produce the same hash code.The algorithm used to generate the hash code is unspecified.
[Note: This method overrides System.Object.GetHashCode. ]
System.Uri Class, System Namespace
public string GetLeftPart(UriPartial part);
Returns the specified portion of the URI represented by the current instance.
- part
- A UriPartial value that specifies the component to return.
A String containing all components up to the specified portion of the URI, or System.String.Empty if the current instance does not contain the component identified by part .
Exception Type Condition ArgumentException The part parameter is not a valid UriPartial value.
The System.Uri.GetLeftPart(System.UriPartial) method returns a string containing the URI components starting with the left-most component of the URI and ending with the component specified by part . The returned string does not include fragment or query information.System.Uri.GetLeftPart(System.UriPartial) includes delimiters as follows:
- System.UriPartial.Scheme has the scheme delimiter added.
- System.UriPartial.Authority does not have the path delimiter added.
- System.UriPartial.Path includes any delimiters in the original URI up to the query or fragment delimiter.
The following example demonstrates the System.Uri.GetLeftPart(System.UriPartial) method.
using System; public class UriTest { public static void Main() { string[] myUri = { "http://www.contoso.com/index.htm", "http:www.contoso.com/index.htm#mark", "mailto:user@contoso.com?subject=uri", "nntp://news.contoso.com/123456@contoso.com" }; foreach (string s in myUri) { Uri aUri = new Uri(s); Console.WriteLine("URI: {0}", aUri.ToString()); Console.WriteLine("Scheme: {0}",aUri.GetLeftPart(UriPartial.Scheme)); Console.WriteLine("Authority: {0}",aUri.GetLeftPart(UriPartial.Authority)); Console.WriteLine("Path: {0}",aUri.GetLeftPart(UriPartial.Path)); } } }The output is
URI: http://www.contoso.com/index.htm
Scheme: http://
Authority: http://www.contoso.com
Path: http://www.contoso.com/index.htm
URI: http://www.contoso.com/index.htm#mark
Scheme: http://
Authority: http://www.contoso.com
Path: http://www.contoso.com/index.htm
URI: mailto:user@contoso.com?subject=uri
Scheme: mailto:
Authority:
Path: mailto:user@contoso.com
URI: nntp://news.contoso.com/123456@contoso.com
Scheme: nntp://
Authority: nntp://news.contoso.com
Path: nntp://news.contoso.com/123456@contoso.com
System.Uri Class, System Namespace
public static string HexEscape(char character);
Converts a specified ASCII character into its escaped hexadecimal equivalent.
- character
- A Char containing the character to convert to escaped hexadecimal representation.
A String containing the escaped hexadecimal representation of the specified character.
Exception Type Condition ArgumentOutOfRangeException The numerical value of character is greater than 255.
The returned string is in the form "%XX", where X represents a hexadecimal digit (0-9, A-F).
System.Uri Class, System Namespace
public static char HexUnescape(string pattern, ref int index);
Converts a specified escaped hexadecimal representation of a character to the character.
- pattern
- A String containing the hexadecimal representation of a character.
- index
- A Int32 containing the location in pattern where the hexadecimal representation of a character begins.
A Char containing a character. If the character pointed to by index is a "%" and there are at least two characters following the "%", and the two characters are valid hexadecimal digits, the hexadecimal digits are converted to Char. Otherwise, the character at index is returned. Valid hexadecimal digits are: 0-9, a-f, A-F.On return, the value of index contains the index of the character following the one returned.
Exception Type Condition ArgumentOutOfRangeException index < 0, or index >= the number of characters in pattern.
System.Uri Class, System Namespace
protected virtual bool IsBadFileSystemCharacter(char character);
Returns a Boolean value that indicates whether the specified character would be an invalid character if used in a file system name.
- character
- A Char containing the character to check.
true
if the specified character is not acceptable for use in a file system name; otherwise,false
.The value returned by this method is implementation-specific.
[Behaviors: This method returnsfalse
if the specified character cannot be used in a URI that identifies a file, as defined by the current file system on the current platform.]
[Default: As described above.]
[Overrides: Override this method to provide a check for invalid characters as defined by the current file system on the current platform.]
[Usage: Use this method to determine if a character can be used in a file name.]
System.Uri Class, System Namespace
protected static bool IsExcludedCharacter(char character);
Returns a Boolean value that indicates whether the specified character is excluded from use or is unwise in URIs, as defined by IETF RFC 2396.
- character
- A Char containing the character to check.
true
if the specified character is required to be escaped; otherwise,false
.
This method returnstrue
for the following characters:
Character(s) Description character < 0x0020 Any character with the ASCII value less than hexadecimal 0x20 (32). character < 0x007f Any character with the ASCII value greater than hexadecimal 0x7f (127). < Less than sign. > Greater than sign. # Number sign (crosshatch, pound sign). % Percent. " Quotation mark.
{ Left curly brace. } Right curly brace. | Pipe sign (vertical bar). \ Backward slash. ^ Circumflex (caret). [ Left square bracket. ] Right square bracket. ` Grave accent.
System.Uri Class, System Namespace
public static bool IsHexDigit(char character);
Returns a Boolean value that indicates whether the specified character is a valid hexadecimal digit.
- character
- A Char containing the character to validate.
true
if the character is a valid hexadecimal digit (0-9, A-F, a-f); otherwisefalse
.
System.Uri Class, System Namespace
public static bool IsHexEncoding(string pattern, int index);
Returns a Boolean value that indicates whether a substring of the specified string is in escaped hexadecimal encoding format ("%" followed by two hexadecimal characters).
- pattern
- The String to check.
- index
- A Int32 containing the location in pattern to check for hex encoding.
true
if the specified location in pattern contains a substring in escaped hexadecimal encoding format; otherwise,false
.
The System.Uri.IsHexEncoding(System.String,System.Int32) method checks for hexadecimal digits case-insensitively.
System.Uri Class, System Namespace
protected virtual bool IsReservedCharacter(char character);
Returns a Boolean value that indicates whether a character is part of the URI reserved set.
true
if character is a URI reserved character as defined by IETF RFC 2396; otherwise,false
.
The following characters are reserved for the use in URI:
[Behaviors: As described above. ]
Character Description ; Semi-colon. / Forward slash. : Colon. @ At sign (commercial at). & Ampersand. = Equals sign. + Plus sign. $ US Dollar sign. , Comma.
[Overrides: Override this method to customize the escaping behavior provided by the Uri type.]
[Usage: Use this method to determine if a character is reserved.]
System.Uri Class, System Namespace
public string MakeRelative(Uri toUri);
Returns the specified Uri as a relative URI.
- toUri
- The URI to compare to the current URI.
A String with the difference between the current instance and toUri if the two URIs are the same except for the path information. If the two URIs differ in more than the System.Uri.AbsolutePath, this method returns the String representation of toUri.
The following example demonstrates the System.Uri.MakeRelative(System.Uri) method.
using System; public class UriTest { public static void Main() { Uri myUri = new Uri("http://www.contoso.com/Hello%20World.htm", true); Console.WriteLine(myUri.ToString()); Console.WriteLine(myUri.MakeRelative(new Uri ("http://www.contoso.com/index.htm"))); } }The output is
http://www.contoso.com/Hello World.htm
index.htm
System.Uri Class, System Namespace
protected virtual void Parse();
Parses the URI into its constituent components.
Exception Type Condition UriFormatException The scheme of the URI is in an invalid format. -or-
The URI is in an invalid form and cannot be parsed.
[Behaviors: This method parses the System.Uri.AbsolutePath property, separates it into various URI components, and stores the components in the appropriate Uri properties. ]
[Default: This method parses path components as defined in IETF RFC 2396. ]
[Overrides: Override this method to provide parsing for URIs in formats that are not defined in IETF RFC 2396. ]
[Usage: Applications typically do not call this method; it is intended for use by the constructors.]
System.Uri Class, System Namespace
public override string ToString();
Returns the unescaped, canonical form of the URI information used to construct the current instance.
A String containing the unescaped, canonical form of the URI represented by the current instance.
The string returned by this method includes the System.Uri.Query and System.Uri.Fragment components.[Note: This method overrides System.Object.ToString.]
System.Uri Class, System Namespace
protected virtual string Unescape(string path);
Converts escape sequences in the specified String into their unescaped equivalents.
- path
- The String to unescape.
A String containing path with its escaped characters converted to their unescaped equivalents. If path isnull
or a zero-length string, returns System.String.Empty.
[Note: Escape sequences can be hex-encoded reserved characters (for example "%40") or hex-encoded UTF-8 sequences (for example "%C4%D2").]
System.Uri Class, System Namespace
public static readonly string SchemeDelimiter;
A String containing the characters that separate the scheme component from the remainder of a URI.
This field is read-only. The value of this field is "://".
System.Uri Class, System Namespace
public static readonly string UriSchemeFile;
A String containing the characters that indicate that a URI identifies a file.
This field is read-only. The value of this field is "file".
System.Uri Class, System Namespace
public static readonly string UriSchemeFtp;
A String containing the characters that indicate that a URI is accessed through the File Transfer Protocol (FTP).
This field is read-only. The value of this field is "ftp".
System.Uri Class, System Namespace
public static readonly string UriSchemeGopher;
A String containing the characters that indicate that a URI is accessed through the Gopher protocol.
This field is read-only. The value of this field is "gopher".
System.Uri Class, System Namespace
public static readonly string UriSchemeHttp;
A String containing the characters that indicate that a URI is accessed through the Hypertext Transfer Protocol (HTTP).
This field is read-only. The value of this field is "http".
System.Uri Class, System Namespace
public static readonly string UriSchemeHttps;
A String containing the characters that indicate that a URI is accessed through the Secure Hypertext Transfer Protocol (HTTPS).
This field is read-only. The value of this field is "https".
System.Uri Class, System Namespace
public static readonly string UriSchemeMailto;
A String containing the characters that indicate that a URI is an email address and is accessed through the Simple Network Mail Protocol (SNMP).
This field is read-only. The value of this field is "mailto".
System.Uri Class, System Namespace
public static readonly string UriSchemeNews;
A String containing the characters that indicate that a URI is an Internet news group and is accessed through the Network News Transport Protocol (NNTP).
This field is read-only. The value of this field is "news".
System.Uri Class, System Namespace
public static readonly string UriSchemeNntp;
A String containing the characters that indicate that a URI is an Internet news group and is accessed through the Network News Transport Protocol (NNTP).
This field is read-only. The value of this field is "nntp".
System.Uri Class, System Namespace
public string AbsolutePath { get; }
Gets the absolute path of the resource identified by the current instance.
A String containing the absolute path to the resource.
This property is read-only.The System.Uri.AbsolutePath property contains the path to the resource identified by the current instance. The System.Uri.AbsolutePath property always returns at least a slash ('/').
If, when the current instance was constructed, the URI was already escaped or the constructor's dontEscape parameter was set to
false
, the value returned by this property is escaped.[Note: The path information does not include the scheme, host name, query, or fragment components of the URI. ]
The following example outputs the absolute path of a URI.
using System; public class UriTest { public static void Main() { Uri myUri = new Uri ("http://www.contoso.com/URI/Hello%20World.htm?date=today", true); Console.WriteLine(myUri.AbsolutePath); } }The output is
/URI/Hello%20World.htm
System.Uri Class, System Namespace
public string AbsoluteUri { get; }
Gets the absolute URI of the resource identified by the current instance in canonical form.
A String containing the URI used to construct the current instance, in canonical format.
This property is read-only.The System.Uri.AbsoluteUri property includes the entire URI stored in the current Uri instance, including any fragment or query information. If, when the current instance was constructed, the URI was already escaped or the constructor's dontEscape parameter was set to
false
, the value returned by this property is escaped.
System.Uri Class, System Namespace
public string Authority { get; }
Gets the authority component of the URI used to construct the current instance.
A String containing the authority component of the current instance. The value returned by this property is composed of the values returned by the System.Uri.Host and System.Uri.Port properties.
This property is read-only.The System.Uri.Authority property returns the System.Uri.Host and System.Uri.Port information specified in the URI used to construct the current instance. The value of this property includes the port information only if the URI specified a port that is not the default for the current scheme. When port information is included in the value returned by this property, the host and port are separated by a colon (":").
System.Uri Class, System Namespace
public string Fragment { get; }
Gets the fragment component of the URI used to construct the current instance.
A String containing any fragment information contained in the URI used to construct the current instance.
This property is read-only.The System.Uri.Fragment property gets any text following a fragment marker ('#') in the URI, including the fragment marker itself. If, when the current instance was constructed, the URI was already escaped or the constructor's dontEscape parameter was set to
false
, the value returned by this property is escaped.[Note: The System.Uri.Fragment property is not considered in a System.Uri.Equals(System.Object) comparison.
]
The following example demonstrates the use of the System.Uri.Fragment property.
using System; public class UriTest { public static void Main() { Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "index.htm#main"); Console.WriteLine(myUri.Fragment); } }The output is
#main
System.Uri Class, System Namespace
public string Host { get; }
Gets the host component of the URI used to construct the current instance.
A String containing the DNS host name or IP address of the host server. If the host information was not specified to the constructor, the value of this property is System.String.Empty .
This property is read-only.If the host information is an IP6 address, the information is enclosed in square brackets ("[" and "]").
The following example demonstrates using the System.Uri.Host property.
using System; public class UriTest { public static void Main() { Uri baseUri = new Uri("http://www.contoso.com:8080/"); Uri myUri = new Uri(baseUri, "shownew.htm?date=today"); Console.WriteLine(myUri.Host); } }The output is
www.contoso.com
System.Uri Class, System Namespace
public UriHostNameType HostNameType { get; }
Gets the format of the host address in the URI used to construct the current instance.
A UriHostNameType that indicates the format of the host address information in the current instance.
This property is read-only.If System.Uri.Host is
null
, the value of this property is System.UriHostNameType.Unknown .
System.Uri Class, System Namespace
public bool IsDefaultPort { get; }
Gets a Boolean value indicating whether the System.Uri.Port value of the current instance is the default port for the scheme of the current instance.
true
if the value in the System.Uri.Port property is the default port for the System.Uri.Scheme ; otherwise,false
.
This property is read-only.[Note: For a list of default port values, see the System.Uri.Port property.]
System.Uri Class, System Namespace
public bool IsFile { get; }
Gets a Boolean value indicating whether the current instance identifies a file.
true
if the resource identified by the current Uri is a file; otherwise,false
.
This property is read-only.The System.Uri.IsFile property is
true
when the System.Uri.Scheme property equals System.Uri.UriSchemeFile.
System.Uri Class, System Namespace
public bool IsLoopback { get; }
Gets a Boolean value indicating whether the host information of the current instance is the current computer.
true
if the host of the current instance is the reserved hostname "localhost" or the loop-back IP address (127.0.0.1); otherwise,false
.
This property is read-only.
The following example demonstrates the System.Uri.IsLoopback property.
using System; public class UriTest { public static void Main() { Uri myUri = new Uri("http://127.0.0.1/index.htm", true); Console.WriteLine("{0} is loopback? {1}", myUri.ToString(), myUri.IsLoopback); myUri = new Uri("http://localhost/index.htm", true); Console.WriteLine("{0} is loopback? {1}", myUri.ToString(), myUri.IsLoopback); } }The output ishttp://127.0.0.1/index.htm is loopback? True
http://localhost/index.htm is loopback? True
System.Uri Class, System Namespace
public string LocalPath { get; }
Gets the local operating-system representation of the resource identified by the current instance.
A String containing the local representation of the resource identified by the current instance.
This property is read-only.If the System.Uri.Scheme of the current instance is not equal to System.Uri.UriSchemeFile, this property returns the same value as System.Uri.AbsolutePath .
If the scheme is equal to System.Uri.UriSchemeFile, this property returns an unescaped platform-dependent local representation of the file name.
System.Uri Class, System Namespace
public string PathAndQuery { get; }
Gets the System.Uri.AbsolutePath and System.Uri.Query components of the URI used to construct the current instance.
A String that contains the values of the System.Uri.AbsolutePath and System.Uri.Query properties.
This property is read-only.
The following example uses the System.Uri.PathAndQuery property to extract the path and query information from a Uri instance.
using System; public class UriTest { public static void Main() { Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today"); Console.WriteLine(myUri.PathAndQuery); } }The output is
/catalog/shownew.htm?date=today
System.Uri Class, System Namespace
public int Port { get; }
Gets the port number used to connect to the System.Uri.Host referenced by the current instance.
A Int32 containing the port number, or -1 if no port is used by the URI System.Uri.Scheme . If no port was specified as part of the URI used to construct the current instance, the System.Uri.Port property returns the default port for the URI scheme.
This property is read-only.[Note: The following table lists the default port number for each supported scheme.
]
Scheme Port file -1 ftp 21 gopher 70 http 80 https 43 mailto 25 news 119 nntp 119
System.Uri Class, System Namespace
public string Query { get; }
Gets the query component of the URI used to construct the current instance.
A String containing the query information included in the specified URI, or System.String.Empty .
This property is read-only.If, when the current instance was constructed, the URI was already escaped or the constructor's dontEscape parameter was set to
false
, the value returned by this property is escaped.[Note: Query information is separated from the path information by a question mark ('?') and is located at the end of a URI. The query information includes the leading question mark.]
The following example uses the System.Uri.Query property to extract the query from a URI.
using System; public class UriTest { public static void Main() { Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today"); Console.WriteLine(myUri.Query); } }The output is
?date=today
System.Uri Class, System Namespace
public string Scheme { get; }
Gets the scheme component of the URI used to construct the current instance.
A String containing the URI scheme.
This property is read-only.
System.Uri Class, System Namespace
public bool UserEscaped { get; }
Gets a Boolean value that indicates whether the URI information used to construct the current instance was escaped before the current instance was created.
true
if the dontEscape parameter of the constructor for the current instance was set totrue
; otherwise,false
.
This property is read-only.
System.Uri Class, System Namespace
public string UserInfo { get; }
Gets the userinfo component of the URI used to construct the current instance.
A String containing any user information included in the URI used to construct the current instance, or System.String.Empty if no user information was included.
This property is read-only.[Note: For details on the userinfo component of a URI, see IETF RFC 2396, 3.2.2.]
System.Uri Class, System Namespace