Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Uri.IsBaseOf(Uri) Method in C#
The Uri.IsBaseOf() method in C# is used to determine whether the current Uri instance is a base of the specified Uri instance. This method is particularly useful when working with URL hierarchies and checking if one URI is a parent or base path of another URI.
Syntax
Following is the syntax −
public bool IsBaseOf(Uri uri);
Parameters
uri − The specified Uri instance to test against the current Uri instance.
Return Value
Returns true if the current Uri instance is a base of the specified Uri; otherwise, false.
How It Works
The IsBaseOf() method returns true when:
-
The current URI's scheme, host, and port match the target URI
-
The current URI's path is a prefix of the target URI's path
-
The current URI represents a logical parent or base of the target URI
Using IsBaseOf() with Same Domain URIs
The following example demonstrates how IsBaseOf() works when comparing URIs from the same domain −
using System;
public class Demo {
public static void Main() {
Uri baseUri = new Uri("https://www.tutorialspoint.com/");
Uri childUri = new Uri("https://www.tutorialspoint.com/index.htm");
Console.WriteLine("Base URI: " + baseUri);
Console.WriteLine("Child URI: " + childUri);
if (baseUri.IsBaseOf(childUri))
Console.WriteLine("Base URI is the base of Child URI");
else
Console.WriteLine("Base URI is not the base of Child URI");
// Test reverse relationship
if (childUri.IsBaseOf(baseUri))
Console.WriteLine("Child URI is the base of Base URI");
else
Console.WriteLine("Child URI is not the base of Base URI");
}
}
The output of the above code is −
Base URI: https://www.tutorialspoint.com/ Child URI: https://www.tutorialspoint.com/index.htm Base URI is the base of Child URI Child URI is not the base of Base URI
Using IsBaseOf() with Different Domains
The following example shows how IsBaseOf() behaves when comparing URIs from different domains −
using System;
public class Demo {
public static void Main() {
Uri uri1 = new Uri("https://www.tutorialspoint.com/csharp/");
Uri uri2 = new Uri("https://www.example.com/csharp/");
Console.WriteLine("URI 1: " + uri1);
Console.WriteLine("URI 2: " + uri2);
if (uri1.IsBaseOf(uri2))
Console.WriteLine("URI 1 is the base of URI 2");
else
Console.WriteLine("URI 1 is not the base of URI 2");
// Test with nested paths on same domain
Uri baseUri = new Uri("https://www.tutorialspoint.com/");
Uri nestedUri = new Uri("https://www.tutorialspoint.com/csharp/methods.htm");
Console.WriteLine("\nBase URI: " + baseUri);
Console.WriteLine("Nested URI: " + nestedUri);
if (baseUri.IsBaseOf(nestedUri))
Console.WriteLine("Base URI is the base of Nested URI");
else
Console.WriteLine("Base URI is not the base of Nested URI");
}
}
The output of the above code is −
URI 1: https://www.tutorialspoint.com/csharp/ URI 2: https://www.example.com/csharp/ URI 1 is not the base of URI 2 Base URI: https://www.tutorialspoint.com/ Nested URI: https://www.tutorialspoint.com/csharp/methods.htm Base URI is the base of Nested URI
Common Use Cases
The IsBaseOf() method is commonly used in scenarios such as:
-
Web scraping − Checking if discovered URLs belong to a specific domain
-
Security validation − Ensuring URLs are within allowed base paths
-
Relative URL resolution − Determining parent-child relationships between URLs
Conclusion
The Uri.IsBaseOf() method provides an efficient way to determine hierarchical relationships between URIs. It returns true when the current URI represents a logical parent or base path of the specified URI, making it valuable for URL validation and navigation logic.
