C# 中的 Uri.MakeRelativeUri(Uri) 方法


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

語法

以下是語法 −

public Uri MakeRelativeUri (Uri uri);

上面 URI 是要與當前 URI 比較的 URI。

示例

現在,我們來看一個實現 Uri.MakeRelativeUri(Uri) 方法的示例 −

using System;
public class Demo {
   public static void Main(){
      Uri newURI1 = new Uri("https://tutorialspoint.tw/");
      Console.WriteLine("URI = "+newURI1);
      Uri newURI2 = new Uri("https://tutorialspoint.tw/java.htm");
      Console.WriteLine("URI = "+newURI2);
      if(newURI1.Equals(newURI2))
         Console.WriteLine("Both the URIs are equal!");
      else
         Console.WriteLine("Both the URIs aren't equal!");
      Uri res = newURI1.MakeRelativeUri(newURI2);
      Console.WriteLine("Relative uri = "+res);
   }
}

輸出

這將產生以下輸出 −

URI = https://tutorialspoint.tw/
URI = https://tutorialspoint.tw/java.htm
Both the URIs aren't equal!
Relative uri = java.htm

示例

現在,我們來看另一個實現 Uri.MakeRelativeUri(Uri) 方法的示例 −

using System;
public class Demo {
   public static void Main(){
      Uri newURI1 = new Uri("https://tutorialspoint.tw/");
      Console.WriteLine("URI = "+newURI1);
      Uri newURI2 = new Uri("https://tutorialspoint.tw/jquery.htm#abcd");
      Console.WriteLine("URI = "+newURI2);
      if(newURI1.Equals(newURI2))
         Console.WriteLine("Both the URIs are equal!");
      else
         Console.WriteLine("Both the URIs aren't equal!");
      Uri res = newURI1.MakeRelativeUri(newURI2);
      Console.WriteLine("Relative uri = "+res);
   }
}

輸出

這將產生以下輸出 −

URI = https://tutorialspoint.tw/
URI = https://tutorialspoint.tw/jquery.htm#abcd
Both the URIs aren't equal!
Relative uri = jquery.htm#abcd

更新於: 2019 年 11 月 7 日

132 次瀏覽

開啟您的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.