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.IsWellFormedOriginalString() Method in C#
The Uri.IsWellFormedOriginalString() method in C# determines whether the original string used to construct a Uri object was well-formed and doesn't require additional escaping. This method returns true if the URI string is properly formatted according to RFC standards, and false otherwise.
This method is particularly useful when validating URI strings before processing them or when you need to ensure that a URI doesn't contain characters that require percent-encoding.
Syntax
Following is the syntax for the IsWellFormedOriginalString() method −
public bool IsWellFormedOriginalString();
Return Value
The method returns a bool value −
-
true− if the original string is well-formed and doesn't need escaping -
false− if the original string contains characters that require escaping or is malformed
Example with Well-Formed URIs
Let us see an example demonstrating the IsWellFormedOriginalString() method with properly formatted URIs −
using System;
public class Demo {
public static void Main() {
Uri newURI1 = new Uri("https://www.tutorialspoint.com/index.htm");
Console.WriteLine("URI = " + newURI1);
Uri newURI2 = new Uri("https://www.qries.com/");
Console.WriteLine("URI = " + newURI2);
if(newURI1.Equals(newURI2))
Console.WriteLine("Both the URIs are equal!");
else
Console.WriteLine("Both the URIs aren't equal!");
if(newURI1.IsWellFormedOriginalString())
Console.WriteLine("newURI1 is well formed!");
else
Console.WriteLine("newURI1 isn't well formed!");
if(newURI2.IsWellFormedOriginalString())
Console.WriteLine("newURI2 is well formed!");
else
Console.WriteLine("newURI2 isn't well formed!");
}
}
The output of the above code is −
URI = https://www.tutorialspoint.com/index.htm URI = https://www.qries.com/ Both the URIs aren't equal! newURI1 is well formed! newURI2 is well formed!
Example with Poorly Formed URIs
This example shows how the method behaves with URIs that contain characters requiring escaping −
using System;
public class Demo {
public static void Main() {
// URI with spaces (requires escaping)
Uri uri1 = new Uri("https://example.com/path with spaces");
Console.WriteLine("URI1: " + uri1);
Console.WriteLine("URI1 well-formed: " + uri1.IsWellFormedOriginalString());
// URI with special characters (requires escaping)
Uri uri2 = new Uri("https://example.com/search?q=hello world");
Console.WriteLine("URI2: " + uri2);
Console.WriteLine("URI2 well-formed: " + uri2.IsWellFormedOriginalString());
// Properly escaped URI
Uri uri3 = new Uri("https://example.com/search?q=hello%20world");
Console.WriteLine("URI3: " + uri3);
Console.WriteLine("URI3 well-formed: " + uri3.IsWellFormedOriginalString());
}
}
The output of the above code is −
URI1: https://example.com/path%20with%20spaces URI1 well-formed: False URI2: https://example.com/search?q=hello%20world URI2 well-formed: False URI3: https://example.com/search?q=hello%20world URI3 well-formed: True
Common Use Cases
The IsWellFormedOriginalString() method is commonly used in scenarios such as −
-
Input validation − checking if user-provided URIs are properly formatted
-
API development − ensuring URI parameters don't require additional processing
-
Web scraping − validating extracted URLs before making HTTP requests
-
Configuration validation − checking application configuration URIs at startup
Conclusion
The Uri.IsWellFormedOriginalString() method provides a simple way to validate whether a URI string is properly formatted and doesn't require percent-encoding. It returns true for well-formed URIs and false for those containing unescaped special characters or spaces.
