[Serializable] |
The Uri class stores only absolute URIs (for example, "http://www.contoso.com/index.htm"). Relative URIs (for example, "/new/index.htm") must be expanded with respect to a base URI so that they are absolute. The Uri.MakeRelative method is provided to convert absolute URIs to relative URIs when necessary.
The Uri constructors will not escape URI strings if the string is a well-formed URI including a scheme identifier that contains escape sequences. The constructors will escape relative URI strings that contain a percent sign (%).
URIs are stored as canonical URIs in escaped encoding, with all characters with ASCII values greater than 127 replaced with their hexidecimal equivalents. To put the URI in canonical form, the Uri constructor performs the following steps.
You can transform the contents of the Uri class from an escape encoded URI reference to a readable URI reference using the Uri.ToString method.
Some URIs include a fragment identifier or query. A fragment identifier is any text that follows a number sign (#) in the URI and is stored in the Uri.Fragment property. Query information is any text that follows a question mark (?) in the URI, and is stored in the Uri.Query property.
Uri siteUri = new Uri("http://www.contoso.com/"); WebRequest wr = WebRequest.Create(siteUri);
ctor #1 | Overloaded:.ctor(string uriString) Initializes a new instance of the Uri class with the specified URI. |
ctor #3 | Overloaded:.ctor(string uriString, bool dontEscape) Initializes a new instance of the Uri class with the specified URI, with control of character escaping. |
ctor #4 | Overloaded:.ctor(Uri baseUri, string relativeUri) Initializes a new instance of the Uri class based on the specified base and relative URIs. |
ctor #5 | Overloaded:.ctor(Uri baseUri, string relativeUri, bool dontEscape) Initializes a new instance of the Uri class based on the specified base and releative URIs, with control of character escaping. |
SchemeDelimiter | Specifies the characters that separate the communication protocol scheme from the address portion of the URI. This field is read-only. |
UriSchemeFile | Specifies that the URI is a pointer to a file. This field is read-only. |
UriSchemeFtp | Specifies that the URI is accessed through the File Transfer Protocol (FTP). This field is read-only. |
UriSchemeGopher | Specifies that the URI is accessed through the Gopher protocol. This field is read-only. |
UriSchemeHttp | Specifies that the URI is accessed through the Hypertext Transfer Protocol (HTTP). This field is read-only. |
UriSchemeHttps | Specifies that the URI is accessed through the Secure Hypertext Transfer Protocol (HTTPS). This field is read-only. |
UriSchemeMailto | Specifies that the URI is an email address and is accessed through the Simple Network Mail Protocol (SNMP). This field is read-only. |
UriSchemeNews | Specifes that the URI is an Internet news group and is accessed through the Network News Transport Protocol (NNTP). This field is read-only. |
UriSchemeNntp | Specifes that the URI is an Internet news group and is accessed through the Network News Transport Protocol (NNTP). This field is read-only. |
AbsolutePath | Read-only Gets the absolute path of the URI. |
AbsoluteUri | Read-only Gets the absolute URI. |
Authority | Read-only Gets the Domain Name System (DNS) host name or IP address and the port number for a server. |
Fragment | Read-only Gets the escaped fragment. |
Host | Read-only Gets the Domain Name System (DNS) host name, or IP address of the server specified in the URI. |
HostNameType | Read-only Returns the type of the host name specified in the URI. |
IsDefaultPort | Read-only Gets a value indicating whether the port value of the URI is the default for this scheme. |
IsFile | Read-only Gets a value indicating whether the specified Uri is a file URI. |
IsLoopback | Read-only Gets a value indicating whether the specified Uri references the local host. |
IsUnc | Read-only Gets a value indicating whether the specified Uri is a universal naming convention (UNC) path. |
LocalPath | Read-only Gets a local operating-system representation of a file name. |
PathAndQuery | Read-only Gets the Uri.AbsolutePath and Uri.Query properties separated by a question mark (?). |
Port | Read-only Gets the port number of the specified URI. |
Query | Read-only Gets any query information included in the specified URI. |
Scheme | Read-only Gets the scheme name of the specified URI. |
Segments | Read-only Gets an array of the segments that make up the specified URI. |
UserEscaped | Read-only Indicates that the URI string was escaped before the Uri instance was created. |
UserInfo | Read-only Gets the user name, password, and other user-specific information associated with the specified URI. |
CheckHostName | Determines whether the specified host name is valid. |
CheckSchemeName | Determines whether the specified scheme name is valid. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
Equals | Overridden: Compares two Uri instances for equality. |
FromHex | Returns the decimal value of a hexadecimal digit. |
GetHashCode | Overridden: Returns the hash code for the specified URI. |
GetLeftPart | Returns the specified portion of a URI. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
HexEscape | Converts a specified character into its hexadecimal equivalent. |
HexUnescape | Converts a specified hexadecimal representation of a character to the character. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
IsHexDigit | Determines whether a specified character is a valid hexadecimal digit. |
IsHexEncoding | Determines whether a string is hexadecimal encoded. |
MakeRelative | Determines the difference between two Uri instances. |
ToString | Overridden: Returns the display string for the specified Uri instance. |
ctor #2 | Overloaded:.ctor(SerializationInfo serializationInfo, StreamingContext streamingContext) Initializes a new instance of the Uri class from the specified instances of the SerializationInfo and StreamingContext classes. |
Canonicalize | |
CheckSecurity | |
Escape | |
EscapeString | Converts a string to its escaped representation. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
IsBadFileSystemCharacter | |
IsExcludedCharacter | |
IsReservedCharacter | |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Parse | |
Unescape |
Hierarchy:
public Uri( |
uriString
Exception Type | Condition |
---|---|
UriFormatException | uriString is empty. -or- The scheme specified in uriString is invalid. -or- uriString contains too many slashes. -or- The password specified in uriString is invalid. -or- The host name specified in uriString is invalid. -or- The file name specified in uriString is invalid. |
Uri myUri = new Uri("http://www.contoso.com/");
protected Uri( |
serializationInfo
streamingContext
uriString
dontEscape
Exception Type | Condition |
---|---|
ArgumentException | uriString is empty. -or- uriString is null. |
UriFormatException | The scheme specified in uriString is invalid. -or- uriString contains too many slashes. -or- The password specified in uriString is invalid. -or- The host name specified in uriString is invalid. -or- The file name specified in uriString is invalid. |
Uri myUri = new Uri("http://www.contoso.com/Hello%20World.htm", true);
baseUri
relativeUri
Exception Type | Condition |
---|---|
UriFormatException | The URI formed by combining baseUri and relativeUri is empty. -or- The scheme specified in the URI formed by combining baseUri and relativeUri is invalid. -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 invalid. -or- The host name specified in the URI formed by combining baseUri and relativeUri is invalid. -or- The file name specified in the URI formed by combining baseUri and relativeUri is invalid. |
Uri baseUri = new Uri("http://www.contoso.com"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm"); Console.WriteLine(myUri.ToString());
public Uri(Uri baseUri, string relativeUri( |
baseUri
relativeUri
dontEscape
Exception Type | Condition |
---|---|
UriFormatException | The URI formed by combining baseUri and relativeUri is empty. -or- The scheme specified in the URI formed by combining baseUri and relativeUri is invalid. -or- Ehe 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 invalid. -or- The host name specified in the URI formed by combining baseUri and relativeUri is invalid. -or- The file name specified in the URI formed by combining baseUri and relativeUri is invalid. |
Uri baseUri = new Uri("http://www.contoso.com"); Uri myUri = new Uri(baseUri, "Hello%20World.htm",true);
public static readonly string SchemeDelimiter;
|
public static readonly string UriSchemeFile;
|
public static readonly string UriSchemeFtp;
|
public static readonly string UriSchemeGopher;
|
public static readonly string UriSchemeHttp;
|
public static readonly string UriSchemeHttps;
|
public static readonly string UriSchemeMailto;
|
public static readonly string UriSchemeNews;
|
public static readonly string UriSchemeNntp;
|
public string AbsolutePath {get;}
|
The path information does not include the scheme, host name, or query portion of the URI. The Uri.AbsolutePath property always returns at least a slash (/).
Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today"); Console.WriteLine(myUri.AbsolutePath);
public string AbsoluteUri {get;}
|
Uri baseUri= new Uri("http://www.contoso.com"); Uri myUri = new Uri(baseUri,"catalog/shownew.htm?date=today"); Console.WriteLine(myUri.AbsoluteUri);
public string Authority {get;}
|
Uri baseUri = new Uri("http://www.contoso.com:8080/"); Uri myUri = new Uri(baseUri,"shownew.htm?date=today"); Console.WriteLine(myUri.Authority);
public string Fragment {get;}
|
The Uri.Fragment property is not considered in any Uri.Equals comparison.
public string Host {get;}
|
Uri baseUri = new Uri("http://www.contoso.com:8080/"); Uri myUri = new Uri(baseUri, "shownew.htm?date=today"); Console.WriteLine(myUri.Host);
public UriHostNameType HostNameType {get;}
|
public bool IsDefaultPort {get;}
|
public bool IsFile {get;}
|
public bool IsLoopback {get;}
|
public bool IsUnc {get;}
|
public string LocalPath {get;}
|
public string PathAndQuery {get;}
|
The Uri.PathAndQuery property is escaped according to RFC 2396.
Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today"); Console.WriteLine(myUri.PathAndQuery);
public int Port {get;}
|
Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri,"catalog/shownew.htm?date=today"); Console.WriteLine(myUri.Port);
public string Query {get;}
|
The query information is escaped according to RFC 2396.
Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today"); Console.WriteLine(myUri.Query); . . . Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today"); Console.WriteLine(myUri.Query);
public string Scheme {get;}
|
Scheme | Description |
---|---|
file | The resource is a file on the local computer. |
ftp | The resource is through FTP. |
gopher | The resource is accessed through the Gopher protocol. |
http | The resource is accessed through HTTP. |
https | The resource is accessed through secure HTTP. |
mailto | The resource is an e-mail address and accessed through SMTP. |
news | The resources is accessed through NNTP. |
Uri baseUri = new Uri("http://www.contoso.com/"); Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today"); Console.WriteLine(myUri.Scheme);
public string[] Segments {get;}
|
public bool UserEscaped {get;}
|
Uri myUri = new Uri("http://www.contoso.com/thick%20and%20thin.htm",true); Console.WriteLine(myUri.UserEscaped);
public string UserInfo {get;}
|
protected virtual void Canonicalize(); |
public static UriHostNameType CheckHostName( |
name
Console.WriteLine(Uri.CheckHostName("www.contoso.com"));
schemeName
protected virtual void CheckSecurity(); |
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
comparand
protected virtual void Escape(); |
str
The string is escaped according to RFC 2396.
~Uri(); |
digit
Exception Type | Condition |
---|---|
ArgumentException | digit is not a valid hexadecimal digit (0-9, a-f, A-F). |
public override int GetHashCode(); |
public string GetLeftPart( |
part
Uri.GetLeftPart includes delimiters in the following cases:
The following examples show a URI and the results of calling Uri.GetLeftPart with UriPartial.Scheme, UriPartial.Authority, or UriPartial.Path.
URI | Scheme | Authority | Path |
---|---|---|---|
http://www.contoso.com/index.htm#main | http:// | http://www.contoso.com | http://www.contoso.com/ |
mailto:user@contoso.com?subject=uri | mailto: | mailto:user@contoso.com | |
nntp://news.contoso.com/123456@contoso.com | nntp:// | nntp://news.contoso.com | nntp://news.contoso.com/123456@contoso.com |
news:123456@contoso.com | news: | news:123456@contoso.com | |
file://server/filename.ext | file:// | file://server | file://server/filename.ext |
file:/filename.ext | file: | file:/filename.ext |
public object GetLifetimeService(); |
public Type GetType(); |
character
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | character is greater than 255. |
pattern
index
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected virtual bool IsBadFileSystemCharacter( |
character
protected static bool IsExcludedCharacter( |
character
character
pattern
index
protected virtual bool IsReservedCharacter( |
character
toUri
protected object MemberwiseClone(); |
protected virtual void Parse(); |
public override string ToString(); |
protected virtual string Unescape( |
path