Uri.Port Uri.Port Uri.Port Uri.Port Property

定义

获取此 URI 的端口号。Gets the port number of this URI.

public:
 property int Port { int get(); };
public int Port { get; }
member this.Port : int
Public ReadOnly Property Port As Integer

属性值

一个包含此 URI 的端口号的 Int32 值。An Int32 value that contains the port number for this URI.

异常

此实例代表一个相对 URI,而此属性仅对绝对 URI 有效。This instance represents a relative URI, and this property is valid only for absolute URIs.

示例

下面的示例将 URI 端口号写入控制台。The following example writes the URI port number to the console. 在这种情况下,值为 HTTP 端口80的默认端口号。In this case, the value is the default port number for HTTP, port 80.

Uri^ baseUri = gcnew Uri( "http://www.contoso.com/" );
Uri^ myUri = gcnew Uri( baseUri,"catalog/shownew.htm?date=today" );
Console::WriteLine( myUri->Port );
Uri baseUri = new Uri("http://www.contoso.com/");
 Uri myUri = new Uri(baseUri,"catalog/shownew.htm?date=today");
 
 Console.WriteLine(myUri.Port);
   
Dim baseUri As New Uri("http://www.contoso.com/")
Dim myUri As New Uri(baseUri, "catalog/shownew.htm?date=today")
       
Console.WriteLine(myUri.Port)

注解

端口号定义用于联系 URI 中引用的服务器的协议端口。The port number defines the protocol port used for contacting the server referenced in the URI. 如果未将端口指定为 URI 的一部分,则Port属性将返回协议的默认值。If a port is not specified as part of the URI, the Port property returns the default value for the protocol. 如果没有默认端口号,则此属性返回-1。If there is no default port number, this property returns -1.

适用于