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

csharpserver side programmingprogramming更新于 2025/9/24 13:07:17

C#中的Uri.MakeRelativeUri(Uri)方法用于确定两个Uri实例之间的差异。

语法

以下是语法−

公共 Uri MakeRelativeUri (Uri uri);

上面的 URI 是要与当前 URI 进行比较的 URI。

示例

现在让我们看一个实现 Uri.MakeRelativeUri(Uri) 方法的示例−

using System;
public class Demo {
   public static void Main(){
      Uri newURI1 = new Uri("https://www.tutorialspoint.com/");
      Console.WriteLine("URI = "+newURI1);
      Uri newURI2 = new Uri("https://www.tutorialspoint.com/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://www.tutorialspoint.com/
URI = https://www.tutorialspoint.com/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://www.tutorialspoint.com/");
      Console.WriteLine("URI = "+newURI1);
      Uri newURI2 = new Uri("https://www.tutorialspoint.com/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://www.tutorialspoint.com/
URI = https://www.tutorialspoint.com/jquery.htm#abcd
Both the URIs aren't equal!
Relative uri = jquery.htm#abcd

相关文章