Uri.MakeRelativeUri(Uri) Method in C#

The Uri.MakeRelativeUri(Uri) method in C# is used to determine the relative difference between two Uri instances. It returns a relative URI that, when resolved against the base URI, yields the target URI.

Syntax

Following is the syntax −

public Uri MakeRelativeUri(Uri uri);

Parameters

  • uri − The URI to compare to the current URI instance. This represents the target URI for which you want to create a relative path.

Return Value

Returns a Uri object that represents the relative URI from the current instance to the specified URI. If the URIs cannot be made relative, it returns the absolute URI.

MakeRelativeUri Concept Base URI example.com/docs/ Target URI example.com/docs/page.html Relative URI page.html

Using MakeRelativeUri for Simple Path Resolution

Example

using System;

public class Demo {
   public static void Main() {
      Uri baseURI = new Uri("https://www.tutorialspoint.com/");
      Console.WriteLine("Base URI = " + baseURI);
      
      Uri targetURI = new Uri("https://www.tutorialspoint.com/java.htm");
      Console.WriteLine("Target URI = " + targetURI);
      
      Uri relativeURI = baseURI.MakeRelativeUri(targetURI);
      Console.WriteLine("Relative URI = " + relativeURI);
      
      Console.WriteLine("URI Type: " + (relativeURI.IsAbsoluteUri ? "Absolute" : "Relative"));
   }
}

The output of the above code is −

Base URI = https://www.tutorialspoint.com/
Target URI = https://www.tutorialspoint.com/java.htm
Relative URI = java.htm
URI Type: Relative

Using MakeRelativeUri with Fragments and Query Strings

Example

using System;

public class Demo {
   public static void Main() {
      Uri baseURI = new Uri("https://www.tutorialspoint.com/");
      Console.WriteLine("Base URI = " + baseURI);
      
      Uri targetURI = new Uri("https://www.tutorialspoint.com/jquery.htm#section1");
      Console.WriteLine("Target URI = " + targetURI);
      
      Uri relativeURI = baseURI.MakeRelativeUri(targetURI);
      Console.WriteLine("Relative URI = " + relativeURI);
      
      // Demonstrating with different base paths
      Uri baseURI2 = new Uri("https://www.tutorialspoint.com/tutorials/");
      Uri targetURI2 = new Uri("https://www.tutorialspoint.com/csharp/index.htm");
      Uri relativeURI2 = baseURI2.MakeRelativeUri(targetURI2);
      Console.WriteLine("Different base relative URI = " + relativeURI2);
   }
}

The output of the above code is −

Base URI = https://www.tutorialspoint.com/
Target URI = https://www.tutorialspoint.com/jquery.htm#section1
Relative URI = jquery.htm#section1
Different base relative URI = ../csharp/index.htm

Using MakeRelativeUri with Different Domains

Example

using System;

public class Demo {
   public static void Main() {
      Uri baseURI = new Uri("https://www.tutorialspoint.com/");
      Console.WriteLine("Base URI = " + baseURI);
      
      Uri differentDomain = new Uri("https://www.example.com/page.html");
      Console.WriteLine("Different domain URI = " + differentDomain);
      
      Uri result = baseURI.MakeRelativeUri(differentDomain);
      Console.WriteLine("Result = " + result);
      Console.WriteLine("Is absolute? " + result.IsAbsoluteUri);
   }
}

The output of the above code is −

Base URI = https://www.tutorialspoint.com/
Different domain URI = https://www.example.com/page.html
Result = https://www.example.com/page.html
Is absolute? True

Common Use Cases

  • Web development − Creating relative links for navigation within a website.

  • File system operations − Generating relative file paths from absolute paths.

  • API development − Converting absolute URLs to relative URLs for resource references.

  • Configuration management − Storing relative paths instead of absolute ones for portability.

Conclusion

The Uri.MakeRelativeUri(Uri) method is essential for creating relative URIs from two absolute URIs that share a common base. It returns a relative URI when possible, or the absolute URI when the URIs cannot be made relative, making it useful for web development and file path operations.

Updated on: 2026-03-17T07:04:35+05:30

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements