-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Refine Uri Host property documentation: clarify IdnHost behavior and focus on Host/IdnHost #12301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6e6673e
95a1139
2ef2e87
d67ef0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| </PropertyGroup> | ||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using System; | ||
|
|
||
| public class UriHostComparison | ||
| { | ||
| public static void Main() | ||
| { | ||
| // <SnippetHostComparison> | ||
| // Demonstrate differences between Host, IdnHost, and DnsSafeHost. | ||
|
|
||
| // Example 1: Regular hostname (ASCII). | ||
| Console.WriteLine("Example 1: Regular ASCII hostname"); | ||
| Uri uri1 = new Uri("http://www.contoso.com:8080/path"); | ||
| Console.WriteLine($" Host: {uri1.Host}"); // www.contoso.com | ||
| Console.WriteLine($" IdnHost: {uri1.IdnHost}"); // www.contoso.com | ||
| Console.WriteLine($" DnsSafeHost: {uri1.DnsSafeHost}"); // www.contoso.com | ||
| Console.WriteLine(); | ||
|
|
||
| // Example 2: International domain name (non-ASCII). | ||
| Console.WriteLine("Example 2: International domain name"); | ||
| Uri uri2 = new Uri("http://münchen.de/path"); | ||
| Console.WriteLine($" Host: {uri2.Host}"); // münchen.de (original) | ||
| Console.WriteLine($" IdnHost: {uri2.IdnHost}"); // xn--mnchen-3ya.de (punycode) | ||
| Console.WriteLine($" DnsSafeHost: {uri2.DnsSafeHost}"); // münchen.de | ||
| Console.WriteLine(); | ||
|
|
||
| // Example 3: IPv6 address without zone ID. | ||
| Console.WriteLine("Example 3: IPv6 address without zone ID"); | ||
| Uri uri3 = new Uri("http://[::1]:8080/path"); | ||
| Console.WriteLine($" Host: {uri3.Host}"); // [::1] (with brackets) | ||
| Console.WriteLine($" IdnHost: {uri3.IdnHost}"); // ::1 (without brackets) | ||
| Console.WriteLine($" DnsSafeHost: {uri3.DnsSafeHost}"); // ::1 (without brackets) | ||
| Console.WriteLine(); | ||
|
|
||
| // Example 4: IPv6 link-local address with zone ID. | ||
| Console.WriteLine("Example 4: IPv6 link-local address with zone ID"); | ||
| Uri uri4 = new Uri("http://[fe80::1%10]:8080/path"); | ||
| Console.WriteLine($" Host: {uri4.Host}"); // [fe80::1] (with brackets, no zone ID) | ||
| Console.WriteLine($" IdnHost: {uri4.IdnHost}"); // fe80::1%10 (without brackets, with zone ID) | ||
| Console.WriteLine($" DnsSafeHost: {uri4.DnsSafeHost}"); // fe80::1%10 (without brackets, with zone ID) | ||
MihaZupan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine(); | ||
|
|
||
| // Example 5: IPv4 address. | ||
| Console.WriteLine("Example 5: IPv4 address"); | ||
| Uri uri5 = new Uri("http://192.168.1.1:8080/path"); | ||
| Console.WriteLine($" Host: {uri5.Host}"); // 192.168.1.1 | ||
| Console.WriteLine($" IdnHost: {uri5.IdnHost}"); // 192.168.1.1 | ||
| Console.WriteLine($" DnsSafeHost: {uri5.DnsSafeHost}"); // 192.168.1.1 | ||
MihaZupan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // </SnippetHostComparison> | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1553,16 +1553,25 @@ The URI formed by combining <paramref name="baseUri" /> and <paramref name="rela | |||||
| </ReturnValue> | ||||||
| <Docs> | ||||||
| <summary>Gets a host name that, after being unescaped if necessary, is safe to use for DNS resolution.</summary> | ||||||
| <value>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.</value> | ||||||
| <value>The host part of the URI in a format suitable for DNS resolution; or the original host string, if it's already suitable for resolution.</value> | ||||||
| <remarks> | ||||||
| <format type="text/markdown"><![CDATA[ | ||||||
|
|
||||||
| ## Remarks | ||||||
| For IPv6 addresses, the brackets ([]) are removed and the <xref:System.Net.IPAddress.ScopeId%2A> property is set, if one was specified when this instance was constructed. | ||||||
|
|
||||||
| 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 any escaped string returned from `DnsSafeHost` before using that string for DNS resolution (see the Example). 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. | ||||||
| If you used an escaped string to construct this instance (for example, `"http://[fe80::200:39ff:fe36:1a2d%254]/temp/example.htm"`), then <xref:System.Uri.DnsSafeHost%2A> returns an escaped string. Unescape any escaped string returned from `DnsSafeHost` before using that string for DNS resolution (see the Example). If you used an invalid unescaped string to construct this instance (for example, "http://[fe80::200:39ff:fe36:1a2d%4]/temp/example.htm"), then <xref:System.Uri.DnsSafeHost%2A> returns an unescaped string. | ||||||
|
|
||||||
| The <xref:System.Uri.DnsSafeHost%2A> property is dependent on configuration settings in .NET Framework apps, as discussed later in this topic. The <xref:System.Uri.IdnHost%2A> property is provided as the preferred alternative to using <xref:System.Uri.DnsSafeHost%2A>, because <xref:System.Uri.IdnHost%2A> is guaranteed to always be DNS safe. | ||||||
| ## Comparison with other Host properties | ||||||
|
|
||||||
| The <xref:System.Uri> class provides two main host properties: | ||||||
|
|
||||||
| - <xref:System.Uri.Host%2A>: Returns the host as it appears in the URI. For IPv6, includes brackets but not the zone ID. | ||||||
| - <xref:System.Uri.IdnHost%2A>: Returns a format suitable for DNS resolution. For valid international domain names, applies punycode encoding. For IPv6, doesn't include brackets but does include the zone ID. | ||||||
|
|
||||||
| The <xref:System.Uri.DnsSafeHost%2A> property is a legacy property that depends on configuration settings. Use <xref:System.Uri.IdnHost%2A> instead for consistent, configuration-independent behavior. | ||||||
|
|
||||||
| The <xref:System.Uri.DnsSafeHost%2A> property is dependent on configuration settings in .NET Framework apps, as discussed later in this topic. The <xref:System.Uri.IdnHost%2A> property is provided as the preferred alternative to using <xref:System.Uri.DnsSafeHost%2A> because <xref:System.Uri.IdnHost%2A> is guaranteed to always be DNS safe. | ||||||
|
||||||
| The <xref:System.Uri.DnsSafeHost%2A> property is dependent on configuration settings in .NET Framework apps, as discussed later in this topic. The <xref:System.Uri.IdnHost%2A> property is provided as the preferred alternative to using <xref:System.Uri.DnsSafeHost%2A> because <xref:System.Uri.IdnHost%2A> is guaranteed to always be DNS safe. | |
| The <xref:System.Uri.DnsSafeHost%2A> property is dependent on configuration settings in .NET Framework apps, as discussed later in this topic. The <xref:System.Uri.IdnHost%2A> property is provided as the preferred alternative to using <xref:System.Uri.DnsSafeHost%2A>, because <xref:System.Uri.IdnHost%2A> typically returns a value that's suitable for DNS resolution. |
Copilot
AI
Feb 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence says IdnHost is "guaranteed to always be DNS safe", but earlier in this section you note that invalid or malformed hostnames might return non-ASCII values, and IPv6 results can include a zone ID. Please remove or qualify the guarantee so it matches the behavior you describe.
| The deprecated <xref:System.Uri.DnsSafeHost%2A> property is dependent on *app.config* settings, which can't be changed by Windows Store applications. <xref:System.Uri.IdnHost%2A> is provided as the preferred alternative to using <xref:System.Uri.DnsSafeHost%2A> because <xref:System.Uri.IdnHost%2A> is guaranteed to always be DNS safe, no matter what the current *app.config* settings might be. | |
| The deprecated <xref:System.Uri.DnsSafeHost%2A> property is dependent on *app.config* settings, which can't be changed by Windows Store applications. <xref:System.Uri.IdnHost%2A> is provided as the preferred alternative to using <xref:System.Uri.DnsSafeHost%2A> because it doesn't depend on *app.config* settings, and it's designed to produce a host value that works for typical DNS resolution scenarios. Callers should still validate or normalize the returned value, especially for malformed hostnames or IPv6 addresses that include a zone ID. |
Uh oh!
There was an error while loading. Please reload this page.