Uri.Isbaseof(Uri) Method In C#

The method Uri.IsBaseOf (Uri) is a part of the System.Uri class in C#. This method is used to determine whether the given Uri is a base for the specified Uri instance. In other words, it checks if the current Uri encompasses or is a prefix of the provided Uri.

Syntax:

It has the following syntax:

Example

public bool IsBaseOf(Uri uri);

The parameter for the method is uri , which is the Uri instance to be compared with the current Uri. The return type is the Boolean. It returns true if the current Uri is a base for the specified 'Uri'; otherwise, it will return false.

Example:

Let us take a C# program to demonstrate the Uri.IsBaseOf(Uri) Method.

Example

using System;
class FileManagementSystem
{
    static void Main()
    {
        // file management system with hierarchical URLs
        Uri adminFolder = new Uri("https://www.filesystem.com/admin/");
        Uri userFolder = new Uri("https://www.filesystem.com/user/");
        Uri specificFile = new Uri("https://www.filesystem.com/user/documents/report.pdf");
        // Using IsBaseOf() method to check relationships
        bool isAdminBase = adminFolder.IsBaseOf(userFolder);
        bool isUserBase = userFolder.IsBaseOf(specificFile);
        Console.WriteLine("In the file management system:");
        Console.WriteLine($"- Admin folder is {(isAdminBase ? "" : "not ")}a base of the User folder.");
        Console.WriteLine($"- User folder is {(isUserBase ? "" : "not ")}a base of the specific file.");
        // Additional Example: Checking if the admin folder is a base for the specific file
        bool isAdminBaseForFile = adminFolder.IsBaseOf(specificFile);
        Console.WriteLine($"- Admin folder is {(isAdminBaseForFile ? "" : "not ")}a base of the specific file.");
    }
}

Output:

Explanation:

In this C# program representing a file management system, hierarchical URLs are simulated using instances of the System.Uri class. The program utilizes the IsBaseOf method to check relationships between different levels within the system. The first set of comparisons determines if the admin folder URL is a base for the user folder URL and if the user folder URL is a base for a specific file URL. After that, the results are displayed and provide clear indications of the hierarchical relationships. Additionally, an extra example examines whether the admin folder URL serves as a base for the specific file URL. The program effectively demonstrates the usage of the Uri.IsBaseOf(Uri) method to evaluate URL hierarchy within the context of a file management system.

Example 2:

Let us take a C# program to illustrate the Uri.IsBaseOf(Uri) Method.

Example

using System;
class WebsiteComparison
{
    static void Main()
    {
        // Checking if two URLs belong to the same domain
        CheckDomain(new Uri("https://www.example.com"), new Uri("https://www.example.com/about"));
        // Checking if one URL is a base for another in terms of subdomains
        CheckSubdomain(new Uri("https://blog.example.com"), new Uri("https://www.blog.example.com/archive"));
    }
    public static void CheckDomain(Uri url1, Uri url2)
    {
        bool sameDomain = url1.IsBaseOf(url2);
        Console.WriteLine($"For domain comparison:");
        Console.WriteLine($"- URL1 ({url1}) is {(sameDomain ? "" : "not ")}a base of URL2 ({url2}).");
        Console.WriteLine();
    }
    // Method to check if one URL is a base for another in terms of subdomains
    public static void CheckSubdomain(Uri url1, Uri url2)
    {
        bool isBase = url1.IsBaseOf(url2);
        Console.WriteLine($"For subdomain comparison:");
        Console.WriteLine($"- URL1 ({url1}) is {(isBase ? "" : "not ")}a base of URL2 ({url2}).");
    }
}

Output:

Explanation:

This C# program illustrates the Uri.IsBaseOf(Uri) method through two comparisons: one checking if two URLs belong to the same domain and another determining if one URL is a base for another in terms of subdomains. The CheckDomain method evaluates the domain relationships, while the CheckSubdomain focuses on subdomain hierarchies, providing clear results for each comparison. The program demonstrates the simplicity and effectiveness of the Uri.IsBaseOf(Uri) method is used to compare different aspects of URLs.

Applications of Uri.IsBaseOf(Uri) method:

Some main practical Applications of this method are as follows:

URL Hierarchy Navigation:

When building web applications, the Uri.IsBaseOf(Uri) method can be used to navigate through hierarchical URL structures. For example, determining if a specific page URL is a subsection of a broader category.

Security Checks:

In security implementations, this method can be employed to validate that certain URLs are within an expected domain or subdomain, preventing unauthorized access.

Link Categorization:

When categorizing hyperlinks or URLs in a web application, developers can utilize this method to establish relationships between different sections of a website.

Example:

Let us take a C# program to demonstrate the above applications.

Example

using System;
class WebApplication
{
    static void Main()
    {
        // Simulating URLs in a web application
        Uri categoryPage = new Uri("https://www.example.com/categories/");
        Uri specificPage = new Uri("https://www.example.com/categories/product123");
        Uri adminPage = new Uri("https://www.example.com/admin/dashboard");
        CheckHierarchyNavigation(categoryPage, specificPage);
        CheckSecurity(adminPage, "admin");
       CategorizeLink(categoryPage, specificPage);
    }
    // Application 1: URL Hierarchy Navigation
    public static void CheckHierarchyNavigation(Uri basePage, Uri subPage)
    {
        bool isBase = basePage.IsBaseOf(subPage);

        Console.WriteLine("Application 1: URL Hierarchy Navigation");
        Console.WriteLine($"- {basePage} is {(isBase ? "" : "not ")}a base of {subPage}.");
        Console.WriteLine();
    }
    // Application 2: Security Checks
    public static void CheckSecurity(Uri allowedDomain, string userInput)
    {
        Uri userInputUri = new Uri($"https://{userInput}.example.com");

        bool isValid = allowedDomain.IsBaseOf(userInputUri);

        Console.WriteLine("Application 2: Security Checks");
        Console.WriteLine($"- {userInputUri} is {(isValid ? "" : "not ")}valid within the expected domain.");
        Console.WriteLine();
    }
    // Application 3: Link Categorization
    public static void CategorizeLink(Uri category, Uri specificPage)
    {
        bool isCategoryLink = category.IsBaseOf(specificPage);

        Console.WriteLine("Application 3: Link Categorization");
        Console.WriteLine($"- {specificPage} is {(isCategoryLink ? "" : "not ")}categorized under {category}.");
    }
}

Output:

Explanation:

This C# program simulates a web application scenario with three distinct applications using the Uri.IsBaseOf(Uri) method. It first checks URL hierarchy navigation by determining if a base page is part of a subpage. The second application performs security checks , validating whether a user-provided input forms a valid URL within an expected domain. Finally, the third application categorizes links , discerning if a specific page is categorized under a broader category page. The program demonstrates the practicality of the Uri.IsBaseOf(Uri) method in diverse web application scenarios.

Input Required

This code uses input(). Please provide values below: