Sai A Sai A
Updated date Jun 30, 2023
In this blog, we will learn how to validate URLs in C# using various methods. This comprehensive blog post covers regular expressions, Uri.TryCreate(), Uri.IsWellFormedUriString(), and more.

Introduction:

URL validation is a common requirement in C# development, especially when dealing with user input or data extraction. This blog post will guide you through various methods to check if a string is a valid URL in C#. We will provide detailed explanations, code examples, and output results for each method. By the end of this article, you will have a solid understanding of URL validation techniques in C# and be able to choose the method that best suits your needs.

Method 1: Regular Expressions

Regular expressions offer a powerful and flexible way to validate URLs in C#. We can utilize the Regex class to match URL patterns. Let's see how it works with an example:

using System;
using System.Text.RegularExpressions;

class Program
{
    static bool ValidateUrlWithRegex(string url)
    {
        string pattern = @"^(https?|ftp)://[^\s/$.?#].[^\s]*$";
        Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
        return regex.IsMatch(url);
    }

    static void Main()
    {
        string url = "https://www.example.com";
        bool isValid = ValidateUrlWithRegex(url);
        Console.WriteLine($"Is valid URL: {isValid}");
    }
}

Output:

Is valid URL: True

Method 2: Uri.TryCreate()

Another approach to validate URLs in C# is by using the Uri.TryCreate() method. This built-in method attempts to create a Uri object from a string representation of a URL. Here's an example:

using System;

class Program
{
    static bool ValidateUrlWithUri(string url)
    {
        return Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult)
            && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
    }

    static void Main()
    {
        string url = "https://www.example.com";
        bool isValid = ValidateUrlWithUri(url);
        Console.WriteLine($"Is valid URL: {isValid}");
    }
}

Output:

Is valid URL: True

Method 3: Uri.IsWellFormedUriString()

The Uri.IsWellFormedUriString() method is another built-in option for URL validation in C#. It checks if a string representation of a URL is well-formed. Let's take a look:

using System;

class Program
{
    static bool ValidateUrlWithWellFormedUriString(string url)
    {
        return Uri.IsWellFormedUriString(url, UriKind.Absolute);
    }

    static void Main()
    {
        string url = "https://www.example.com";
        bool isValid = ValidateUrlWithWellFormedUriString(url);
        Console.WriteLine($"Is valid URL: {isValid}");
    }
}

Output:

Is valid URL: True

Conclusion:

In this blog post, we explored different methods for validating URLs in C#. We started with regular expressions, which provide a flexible approach for pattern matching. We then covered the Uri.TryCreate() method, which allows us to create a Uri object from a URL string and validate its scheme. Finally, we explored the Uri.IsWellFormedUriString() method, which checks if a URL string is well-formed.

Comments (0)

There are no comments. Be the first to comment!!!