Home » C#

C# | Uri.MakeRelativeUri() Method with Example

Uri.MakeRelativeUri() Method: Here, we are going to learn about the MakeRelativeUri() method of Uri class with example in C#.
Submitted by Nidhi, on March 26, 2020

Uri.MakeRelativeUri() Method

Uri.MakeRelativeUri() method is an instance method of Uri class. This method checks the difference between the current of the object and the specified object and returns the object of the Uri class.

Syntax:

    Uri Uri.MakeRelativeUri(Uri uri);

Parameter(s):

  • Uri uri – represents an object to be checked with current object to determine difference.

Return value:

The return type of this method is Uri, 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.

Exception:

    System.InvalidOperationException;

Example to demonstrate example of Uri.MakeRelativeUri() method

using System;

class UriExample
{
    //Entry point of Program
    static public void Main()
    {
        // Create some Uri objects
        Uri uri = new Uri("https://www.includehelp.com/");
        
        // Create a new Uri to check above uri are base of this uri or not.
        Uri newUri = new Uri("https://www.includehelp.com/dot-net/c-sharp-programs.aspx");

        string diffUri = uri.MakeRelativeUri(newUri).ToString();
        
        Console.WriteLine("Difference of uri and newUri is: "+diffUri);
        
    }
}

Output

Difference of uri and newUri is: dot-net/c-sharp-programs.aspx

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.uri.makerelativeuri?view=netstandard-2.1



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.