Uri.DnsSafeHost Uri.DnsSafeHost Uri.DnsSafeHost Uri.DnsSafeHost Property


获得可安全用于 DNS 解析的未转义主机名(如必要)。Gets a host name that, after being unescaped if necessary, is safe to use for DNS resolution.

 property System::String ^ DnsSafeHost { System::String ^ get(); };
public string DnsSafeHost { get; }
member this.DnsSafeHost : string
Public ReadOnly Property DnsSafeHost As String


一个String , 该字符串包含 URI 的主机部分, 格式适用于 DNS 解析; 如果该字符串已适用于解析, 则为原始主机字符串。A String that contains the host part of the URI in a format suitable for DNS resolution; or the original host string, if it is already suitable for resolution.


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


下面的示例从字符串Uri创建实例。The following example creates a Uri instance from a string. 它说明了从Host返回的值 (返回 URI 中指定的主机名或地址) 与从DnsSafeHost返回的值之间的差异, 后者返回在 DNS 解析中可安全使用的地址。It illustrates the difference between the value returned from Host, which returns the host name or address specified in the URI, and the value returned from DnsSafeHost, which returns an address that is safe to use in DNS resolution.

// Create new Uri using a string address.         
Uri^ address = gcnew Uri( "http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm" );

// Make the address DNS safe. 
// The following outputs "[fe80::200:39ff:fe36:1a2d]".
Console::WriteLine( address->Host );

// The following outputs "fe80::200:39ff:fe36:1a2d%254".
Console::WriteLine( address->DnsSafeHost );
// Create new Uri using a string address.         
Uri address = new Uri("http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm");

// Make the address DNS safe. 

// The following outputs "[fe80::200:39ff:fe36:1a2d]".

// The following outputs "fe80::200:39ff:fe36:1a2d%254".
    ' Create new Uri using a string address.         
    Dim address As New Uri("http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm")
    ' Make the address DNS safe. 
    ' The following outputs "[fe80::200:39ff:fe36:1a2d]".
    ' The following outputs "fe80::200:39ff:fe36:1a2d%254".

End Sub 'SampleDNSSafeHost

如 "备注" 中所述, 在解析之前 unescape 主机名。As explained in Remarks, unescape the host name before resolving it. 你可以使用UnescapeDataString方法 unescape 主机名, 并且可以通过GetHostEntry调用方法来解决该问题。You can use the UnescapeDataString method to unescape the host name, and you can resolve it by calling the GetHostEntry method.


对于 IPv6 地址, 删除方括号 ([]), 并ScopeId设置属性 (如果在构造此实例时指定了一个属性)。For IPv6 addresses, the brackets ([]) are removed and the ScopeId property is set, if one was specified when this instance was constructed.

如果使用了转义字符串构造此实例 (例如"http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"), 则 uri.dnssafehost 将返回一个转义字符串。If you used an escaped string to construct this instance (for example, "http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"), then DnsSafeHost returns an escaped string. Unescape 在使用该字符串进行DnsSafeHost DNS 解析之前返回的任何转义字符串 (请参阅示例)。Unescape any escaped string returned from DnsSafeHost before using that string for DNS resolution (see the Example). 如果你使用了无效的非转义字符串来构造此实例 (例如, "http://[fe80:: 200: 39ff: fe36: 1a2d% 4]/temp/example.htm"), 则 Uri.dnssafehost 将返回未转义的字符串。If you used an invalid unescaped string to construct this instance (for example, "http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm"), then DnsSafeHost returns an unescaped string.

DnsSafeHost属性依赖于配置设置, 如本主题后面所述。The DnsSafeHost property is dependent on configuration settings, as discussed later in this topic. Windows 应用商店应用程序无法更改配置设置, 这可能会在使用DnsSafeHost时导致不一致的结果。Configuration settings cannot be changed by Windows Store applications, which can lead to inconsistent results when using DnsSafeHost. 提供IdnHost的属性是使用DnsSafeHost的首选替代方法, 因为IdnHost无论当前的app.config设置如何, 保证始终是 DNS 安全的。The IdnHost property is provided as the preferred alternative to using DnsSafeHost, because IdnHost is guaranteed to always be DNS safe, no matter what the current app.config settings might be.

DnsSafeHost属性已在 .NET Framework v 3.5、3.0 SP1 和 2.0 SP1 中进行了扩展, 以便基于 RFC 3987 提供国际资源标识符 (IRI) 支持。The DnsSafeHost property has been extended in .NET Framework v3.5, 3.0 SP1, and 2.0 SP1 to provide International Resource Identifier (IRI) support based on RFC 3987. 除非当前用户专门启用 IRI,否则他们看不到任何 NET Framework 2.0 行为的改变。Current users will not see any change from the .NET Framework 2.0 behavior unless they specifically enable IRI. 这确保了 NET Framework 以前版本的应用程序兼容性。This ensures application compatibility with prior versions of the .NET Framework.

若要启用对 IRI 的支持, 需要以下两项更改:To enable support for IRI, the following two changes are required:

  1. 将以下行添加到 .NET Framework 2.0 目录下的 machine.config 文件中 Add the following line to the machine.config file under the .NET Framework 2.0 directory

    <节名称 = "uri" type = "UriSection, System, Version =, Culture = 中性, PublicKeyToken = b77a5c561934e089"/><section name="uri" type="System.Configuration.UriSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

  2. 指定是否要将国际化域名 (IDN) 分析应用于域名, 以及是否应该应用 IRI 分析规则。Specify whether you want Internationalized Domain Name (IDN) parsing applied to the domain name and whether IRI parsing rules should be applied. 可以在machine.config 或 app.config 文件中完成此操作。This can be done in the machine.config or in the app.config file. 例如, 添加以下内容:For example, add the following:

      <idn enabled="All" />  
      <iriParsing enabled="true" />  

启用 IDN 可以将域名中所有 Unicode 标签转换成标签的 Punycode 等同项。Enabling IDN will convert all Unicode labels in a domain name to their Punycode equivalents. Punycode 名称只包含 ASCII 字符,并且始终以 xn-- 前缀开头。Punycode names contain only ASCII characters and always start with the xn-- prefix. 这样是为了支持 Internet 上的 DNS 服务器,因为大部分 DNS 服务器仅支持 ASCII 字符(参见 RFC 3940)。The reason for this is to support existing DNS servers on the Internet, since most DNS servers only support ASCII characters (see RFC 3940).

启用 IDN 只会影响DnsSafeHost属性的值。Enabling IDN only affects the value of the DnsSafeHost property.

根据所使用的 DNS 服务器, IDN 有三个可能的值:There are three possible values for IDN depending on the DNS servers that are used:

  • 已启用 idn = 全部idn enabled = All

    此值会将所有 Unicode 域名转换为它们的 Punycode 等效项(IDN 名称)。This value will convert any Unicode domain names to their Punycode equivalents (IDN names).

  • 启用 idn = AllExceptIntranetidn enabled = AllExceptIntranet

    此值会将所有外部 Unicode 域名转换为使用 Punycode 等效项(IDN 名称)。This value will convert all external Unicode domain names to use the Punycode equivalents (IDN names). 在这种情况下,若要处理本地 Intranet 上的国际化名称,用于 Intranet 的 DNS 服务器应该支持 Unicode 名称。In this case to handle international names on the local Intranet, the DNS servers that are used for the Intranet should support Unicode names.

  • 启用 idn = 无idn enabled = None

    此值不会将任何 Unicode 域名转换为使用 Punycode。This value will not convert any Unicode domain names to use Punycode. 这是与 .NET Framework 2.0 行为一致的默认值。This is the default value which is consistent with the .NET Framework 2.0 behaviour.

启用 IRI 分析 (启用 iriparsing> = true) 将根据 RFC 3987 中的最新 IRI 规则执行规范化和字符检查。Enabling IRI parsing (iriParsing enabled = true) will do normalization and character checking according to the latest IRI rules in RFC 3987. 默认值为false , 将根据 rfc 2396 和 rfc 2732 (针对 IPv6 文本) 执行规范化和字符检查。The default value is false and will do normalization and character checking according to RFC 2396 and RFC 2732 (for IPv6 literals).

有关 IRI 支持的详细信息, 请参阅Uri类的 "备注" 部分。For more information on IRI support, see the Remarks section for the Uri class.