📜  C#中的Uri.DnsSafeHost属性以及示例

📅  最后修改于: 2021-05-29 21:49:43             🧑  作者: Mango

Uri.DnsSafeHost属性是实例属性,用于获取未转义的主机名,该主机名可以安全地用于DNS解析。

句法 :

public string DnsSafeHost { get; }

返回值:该属性返回字符串 ,它以适合DNS解析的格式返回URI的主机部分,如果已经适合解析,则返回原始主机字符串。

异常:如果实例是相对URI,并且此属性仅对绝对URI有效,则此属性将提供InvalidOperationException

范例1:

C#
// C# program to demonstrate the
// Uri.DnsSafeHost property
using System;
using System.Globalization;
  
class GFG {
  
  // Main Method
public static void Main() {
    // Declaring and initializing value1
    Uri v1 = new Uri("https://www.geeksforgeeks.org/greedy-algorithms/#standardGreedyAlgorithms");
  
    // using DnsSafeHost property
    Console.WriteLine("Uri DnsSafeHost: " + v1.DnsSafeHost);
  }
}


C#
// C# program to demonstrate the  
// Uri.DnsSafeHost property  
using System;  
using System.Globalization;  
      
class GFG {  
      
     // Main Method  
    public static void Main()  
    {  
        // Declaring and initializing value1  
        Uri  v1 = new Uri("https://docs.microsoft.com/en-us/dotnet/api/system.uri.isloopback?view=netcore-3.1");
          
        // using DnsSafeHost property  
        Console.WriteLine("Hostname: "+ v1.DnsSafeHost);  
    }  
}


输出:

Hostname: www.geeksforgeeks.org

范例2:

C#

// C# program to demonstrate the  
// Uri.DnsSafeHost property  
using System;  
using System.Globalization;  
      
class GFG {  
      
     // Main Method  
    public static void Main()  
    {  
        // Declaring and initializing value1  
        Uri  v1 = new Uri("https://docs.microsoft.com/en-us/dotnet/api/system.uri.isloopback?view=netcore-3.1");
          
        // using DnsSafeHost property  
        Console.WriteLine("Hostname: "+ v1.DnsSafeHost);  
    }  
}

输出:

Hostname: docs.microsoft.com`