เมธอด Uri.MakeRelativeUri(Uri) ใน C# ใช้กำหนดความแตกต่างระหว่างสองอินสแตนซ์ Uri
ไวยากรณ์
public Uri MakeRelativeUri (Uri uri);
พารามิเตอร์ uri คือ URI ที่ต้องการเปรียบเทียบกับ URI ปัจจุบัน
ตัวอย่าง
ตัวอย่างที่ 1: เปรียบเทียบ URI ฐานกับหน้า java.htm
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
ตัวอย่างเพิ่มเติม
ตัวอย่างที่ 2: เปรียบเทียบ URI ฐานกับหน้า jquery.htm#abcd
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