📜  C#| Uri.MakeRelativeUri(Uri)方法

📅  最后修改于: 2021-05-30 00:37:36             🧑  作者: Mango

Uri.MakeRelativeUri(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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.uri.makerelativeuri?view=netstandard-2.1