Uri.DnsSafeHost Property

July 28, 2014

Gets an unescaped host name that is safe to use for DNS resolution.

Namespace:  System
Assembly:  System (in System.dll)

public string DnsSafeHost { get; }

Property Value

Type: System.String
A String that contains the unescaped host part of the URI that is suitable for DNS resolution; or the original unescaped host string, if it is already suitable for resolution.

ExceptionCondition
InvalidOperationException

This property is valid only for an absolute Uri instance.

For IPv6 addresses, the brackets ([]) are removed and the ScopeId property is set, if one was specified when this instance was constructed.

The following example creates a Uri instance from a string. 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 = new Uri("http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm");

// Make the address DNS safe. 

// The following outputs "[fe80::200:39ff:fe36:1a2d]".
outputBlock.Text += address.Host;
outputBlock.Text += "\n";

// The following outputs "fe80::200:39ff:fe36:1a2d%4".
outputBlock.Text += address.DnsSafeHost;
outputBlock.Text += "\n";


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft