C# में Uri.MakeRelativeUri(Uri) मेथड दो Uri इंस्टेंस के बीच का अंतर निर्धारित करने के लिए उपयोग किया जाता है। यह पहले URI के सापेक्ष दूसरे URI का सापेक्ष पथ लौटाता है।
सिंटैक्स
public Uri MakeRelativeUri (Uri uri);
पैरामीटर uri वह URI है जिसकी तुलना वर्तमान URI से की जानी है।
उदाहरण 1: सरल पथ अंतर
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: क्वेरी और फ्रैगमेंट के साथ
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
उदाहरण 3: अलग डोमेन (पूर्ण URI लौटाता है)
using System;
public class Demo
{
public static void Main()
{
Uri baseUri = new Uri("https://www.example.com/base/");
Uri targetUri = new Uri("https://www.other.com/page.htm");
Uri relative = baseUri.MakeRelativeUri(targetUri);
Console.WriteLine("Relative uri = " + relative);
// आउटपुट: https://www.other.com/page.htm (पूर्ण URI क्योंकि अलग डोमेन)
}
}
महत्वपूर्ण नियम
- यदि दोनों URI का स्कीम और होस्ट समान है, तो सापेक्ष पथ लौटाया जाता है
- यदि स्कीम या होस्ट अलग है, तो पूर्ण लक्ष्य URI लौटाया जाता है
- लौटाया गया मान हमेशा एक सापेक्ष URI होता है (UriKind.Relative)
- यह मेथड केस-इनसेंसिटिव तुलना करता है स्कीम और होस्ट के लिए
उपयोग के मामले
- वेब एप्लिकेशन में सापेक्ष लिंक जनरेट करना
- संसाधन पथों की तुलना करना
- ब्रेडक्रंब नेविगेशन बनाना
- साइटमैप जनरेशन