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:

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.

Output:

Uri.IsBaseOf(Uri) Method in C#

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.

Output:

Uri.IsBaseOf(Uri) Method in C#

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.

Output:

Uri.IsBaseOf(Uri) Method in C#

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.






Latest Courses