當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# Uri.MakeRelativeUri(Uri)用法及代碼示例

Uri.MakeRelativeUri(Uri)方法用於確定兩個Uri實例之間的差異。

用法: public Uri MakeRelativeUri (Uri uri);
Here, it takes the URI to compare to the current URI.

返回值:如果此URI實例的主機名和方案與uri相同,則此方法返回一個相對Uri,將其附加到當前URI實例後產生uri。如果主機名或方案不同,則此方法返回代表uri參數的Uri。


異常:

  • ArgumentNullException:如果uri為null。
  • UriFormatException:如果此實例表示相對URI,並且此屬性僅對絕對URI有效。

以下示例程序旨在說明Uri.MakeRelativeUri(Uri)方法的用法:

示例1:

// C# program to demonstrate the 
// Uri.MakeRelativeUri() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Create a base Uri. 
        Uri address1 = new Uri("http://www.contoso.com/"); 
  
        // Create a new Uri from a string. 
        Uri address2 = new Uri("http://www.contoso.com/index.htm?date=today"); 
  
        // Determining the difference  
        // between address1 and address2. 
        // using MakeRelativeUri() method 
  
        Uri uri = address1.MakeRelativeUri(address2); 
  
        // Displaying the result 
        Console.WriteLine("relative uri is : {0}", uri); 
    } 
}
輸出:
relative uri is : index.htm?date=today

示例2:對於ArgumentNullException

// C# program to demonstrate the 
// Uri.MakeRelativeUri() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Create a base Uri. 
            Uri address1 = new Uri("http://www.contoso.com/"); 
  
            // Create a new Uri from a string. 
            Uri address2 = null; 
  
            // Determining the difference  
            // between address1 and address2. 
            // using MakeRelativeUri() method 
            Uri uri = address1.MakeRelativeUri(address2); 
  
            // Displaying the result 
            Console.WriteLine("relative uri is : {0}", uri); 
        } 
  
        catch (ArgumentNullException e)  
        { 
            Console.WriteLine("uri should not be null"); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
uri should not be null
Exception Thrown: System.ArgumentNullException

示例3:對於UriFormatException

// C# program to demonstrate the 
// Uri.MakeRelativeUri() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Create a base Uri. 
            Uri address1 = new Uri("http://www.contoso.com/"); 
  
            // Determining the difference 
            // between address1 and address2. 
            // using MakeRelativeUri() method 
  
            Uri uri = address1.MakeRelativeUri(new Uri("http:://www.contoso.com/??index.htm?date=today")); 
  
            // Displaying the result 
            Console.WriteLine("relative uri is : {0}", uri); 
        } 
        catch (ArgumentNullException e) { 
            Console.WriteLine("uri should not be null"); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
        catch (UriFormatException e) { 
            Console.WriteLine("uri should be in correct format"); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
uri should be in correct format
Exception Thrown: System.UriFormatException

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Uri.MakeRelativeUri(Uri) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。