当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Uri.IsWellFormedOriginalString()用法及代码示例


Uri.IsWellFormedOriginalString()方法用于显示用于构造此Uri的字符串是否为well-formed,并且不需要进一步转义。

用法: public bool IsWellFormedOriginalString ();

返回值:如果字符串为well-formed,则此方法返回true,否则返回false。


以下示例程序旨在说明Uri.IsWellFormedOriginalString()方法的使用:

示例1:

// C# program to demonstrate the 
// Uri.IsWellFormedOriginalString() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing address 
        Uri address1 = new Uri("http://www.contoso.com/index.htm#search"); 
  
        // Validating if Uri is well formed or not 
        // using IsWellFormedOriginalString() method 
        bool value = address1.IsWellFormedOriginalString(); 
  
        // Displaying the result 
        if (value) 
            Console.WriteLine("uri is well formed"); 
        else
            Console.WriteLine("uri is not well formed"); 
    } 
}
输出:
uri is well formed

示例2:

// C# program to demonstrate the 
// Uri.IsWellFormedOriginalString()  
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        try { 
  
            // calling get() method 
            get(new Uri("http://www.contoso.com/path")); 
  
            // if The string is not correctly escaped. 
            get(new Uri("http://www.contoso.com/path???/file name")); 
  
            // The string is an absolute Uri  
            // that represents an implicit  
            // file Uri. 
            get(new Uri("c:\\directory\filename")); 
  
            // The string is an absolute URI that 
            // is missing a slash before the path. 
            get(new Uri("file://c:/directory/filename")); 
  
            // The parser for the Uri.Scheme indicates  
            // that the original string was not well-formed. 
            get(new Uri("xy11.z://www.contoso.com/path/file")); 
  
            // The string contains unescaped backslashes 
            // even if they are treated as forwarding slashes. 
            get(new Uri("http:\\host/path/file"));  
  
            // The string represents a hierarchical  
            // absolute Uri and does not contain "://". 
            get(new Uri("www.contoso.com/path/file")); 
        } 
  
        catch (UriFormatException e)  
        { 
            Console.Write("uri is so poorly formed that system thrown "); 
            Console.Write("{0}", e.GetType(), e.Message); 
            Environment.Exit(1); 
        } 
    } 
  
    // defining get() method 
    public static void get(Uri address) 
    { 
        // Validating if Uri is well formed or not 
        // using IsWellFormedOriginalString() method 
        bool value = address.IsWellFormedOriginalString(); 
  
        // Displaying the result 
        if (value) 
            Console.WriteLine("uri is well formed"); 
        else
            Console.WriteLine("uri is poorly formed"); 
    } 
}
输出:
uri is well formed
uri is poorly formed
uri is poorly formed
uri is poorly formed
uri is well formed
uri is so poorly formed that system thrown System.UriFormatException

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Uri.IsWellFormedOriginalString() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。