Open In App

C# | Uri.MakeRelativeUri(Uri) Method

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Uri.MakeRelativeUri(Uri) Method is used to determine the difference between two Uri instances.
Syntax: public Uri MakeRelativeUri (Uri uri); Here, it takes the URI to compare to the current URI. Return Value: If the hostname and scheme of this URI instance and uri are the same, then this method returns a relative Uri that, when appended to the current URI instance, yields uri. If the hostname or scheme is different, then this method returns a Uri that represents the uri parameter.
Exceptions:
  • ArgumentNullException: If uri is null.
  • UriFormatException: If this instance represents a relative URI, and this property is valid only for absolute URIs.
Below programs illustrate the use of Uri.MakeRelativeUri(Uri) Method: Example 1: csharp
// C# program to demonstrate the
// Uri.MakeRelativeUri() Method
using System;
using System.Globalization;

class GFG {

    // Main Method
    public static void Main()
    {
        // Create a base Uri.
        Uri address1 = new Uri("https://www.microsoft.com/en-us/);

        // Create a new Uri from a string.
        Uri address2 = new Uri("https://www.microsoft.com/en-us/?date=today");

        // Determining the difference 
        // between address1 and address2.
        // using MakeRelativeUri() method

        Uri uri = address1.MakeRelativeUri(address2);

        // Displaying the result
        Console.WriteLine("relative uri is : {0}", uri);
    }
}
Output:
relative uri is : index.htm?date=today
Example 2: For ArgumentNullException csharp
// C# program to demonstrate the
// Uri.MakeRelativeUri() Method
using System;
using System.Globalization;

class GFG {

    // Main Method
    public static void Main()
    {
        try {

            // Create a base Uri.
            Uri address1 = new Uri("https://www.microsoft.com/en-us/);

            // Create a new Uri from a string.
            Uri address2 = null;

            // Determining the difference 
            // between address1 and address2.
            // using MakeRelativeUri() method
            Uri uri = address1.MakeRelativeUri(address2);

            // Displaying the result
            Console.WriteLine("relative uri is : {0}", uri);
        }

        catch (ArgumentNullException e) 
        {
            Console.WriteLine("uri should not be null");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
Output:
uri should not be null
Exception Thrown: System.ArgumentNullException
Example 3: For UriFormatException csharp
// C# program to demonstrate the
// Uri.MakeRelativeUri() Method
using System;
using System.Globalization;

class GFG {

    // Main Method
    public static void Main()
    {
        try {

            // Create a base Uri.
            Uri address1 = new Uri("https://www.microsoft.com/en-us/);

            // Determining the difference
            // between address1 and address2.
            // using MakeRelativeUri() method

            Uri uri = address1.MakeRelativeUri(new Uri("http:://www.contoso.com/??index.htm?date=today"));

            // Displaying the result
            Console.WriteLine("relative uri is : {0}", uri);
        }
        catch (ArgumentNullException e) {
            Console.WriteLine("uri should not be null");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (UriFormatException e) {
            Console.WriteLine("uri should be in correct format");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
Output:
uri should be in correct format
Exception Thrown: System.UriFormatException
Reference:

Similar Reads