If the string that was used to create this Uri was well-formed and does not need to be further escaped, it can be determined using the Uri.IsWellFormedOriginalString method. Working with URIs (Uniform Resource Identifiers) is made possible by the C# Uri class, which includes the Uri.IsWellFormedOriginalString method. This method is used to ascertain whether a URI string is well-formed with reference to the URI syntax rules outlined in RFC 3986.
Syntax:
It has the following syntax:
public bool IsWellFormedOriginalString ();
The method's return value is either false or true depending on whether the string was well-formed.
Working of the Uri.IsWellFormedOriginalString method:
- URI Well-Formedness: A URI that complies with the URI specification's syntax rules is said to be well-formed. The proper formatting of the query, fragment, scheme, authority, and path components is one of these rules.
- Method Objective: The IsWellFormedOriginalString method determines if a URI's string representation complies with these syntax requirements. It confirms whether the URI string is correctly structured and can be parsed without errors.
- Return Value: Whether the URI string supplied to this method is well-formed is indicated by the Boolean value (true or false) returned by this method. It returns false otherwise; if the URI is not malformed, it returns true.
- Usage: Before processing or utilizing user-provided or dynamically generated URI strings for network operations, developers usually employ this technique to verify their validity.
- When URI strings are used in web requests, parsing, or other URI-related operations, IsWellFormedOriginalString is a helpful function to validate that they follow standard syntax.
Example:
string uriString = "https://www.example.com/page?param=value";
Uri uri = new Uri(uriString);
bool isWellFormed = uri.IsWellFormedOriginalString();
Console.WriteLine("Is URI Well-Formed? " + isWellFormed);
// Output: Is URI Well-Formed? True
Example 1:
Let us take an example to illustrate the Uri.IsWellFormedOriginalString method in C#.
using System;
using System.Globalization;
class Program
{
// Main Method
public static void Main()
{
// Declaring and initializing a URI with a modified address
Uri address = new Uri("https://www.example.org/document?id=123");
// Validating if the URI is well-formed or not using IsWellFormedOriginalString() method
bool isWellFormed = address.IsWellFormedOriginalString();
// Displaying the result
if (isWellFormed)
Console.WriteLine("The URI is well-formed.");
else
Console.WriteLine("The URI is not well-formed.");
}
}
Output:
The URI is well-formed.
Example 2:
Let us take another example to illustrate the Uri.IsWellFormedOriginalString method in C#.
using System;
class Program
{
// Main Method
public static void Main()
{
try
{
// Calling the get() method with different URIs
CheckUri(new Uri("https://www.example.com/resource"));
CheckUri(new Uri("https://www.example.com/resource???/file name"));
CheckUri(new Uri("file:///C:/directory/filename"));
CheckUri(new Uri("file://c:/directory/filename"));
CheckUri(new Uri("xy11.z://www.example.com/resource/file"));
CheckUri(new Uri("http:\\\\host/path/file"));
CheckUri(new Uri("www.example.com/resource/file"));
}
catch (UriFormatException e)
{
Console.Write("The URI is so poorly formed that the system thrown ");
Console.Write("{0}", e.GetType(), e.Message);
Environment.Exit(1);
}
}
// Defining the CheckUri() method
public static void CheckUri(Uri address)
{
// Validating if the URI is well-formed or not using IsWellFormedOriginalString() method
bool isWellFormed = address.IsWellFormedOriginalString();
// Displaying the result
if (isWellFormed)
Console.WriteLine("The URI is well-formed.");
else
Console.WriteLine("The URI is poorly formed.");
}
}
Output:
The URI is well-formed.
The URI is poorly formed.
The URI is well-formed.
The URI is poorly formed.
The URI is well-formed.
The URI is so poorly formed that the system thrown System.UriFormatException
Example 3:
Let us take another example to illustrate the Uri.IsWellFormedOriginalString method in C#.
using System;
public class Program {
public static void Main() {
// Creating and displaying the first URI
Uri uri1 = new Uri("https://www.example.com/resource1");
Console.WriteLine("First URI: " + uri1);
// Creating and displaying the second URI
Uri uri2 = new Uri("https://www.example.org/");
Console.WriteLine("Second URI: " + uri2);
// Checking if the URIs are equal
if (uri1.Equals(uri2))
Console.WriteLine("The URIs are equal.");
else
Console.WriteLine("The URIs are not equal.");
// Checking if the first URI is well-formed
if (uri1.IsWellFormedOriginalString())
Console.WriteLine("The first URI is well-formed.");
else
Console.WriteLine("The first URI is not well-formed.");
}
}
Output:
First URI: https://www.example.com/resource1
Second URI: https://www.example.org/
The URIs are not equal.
The first URI is well-formed.
CONCLUSION:
In conclusion, we can use the C# Uri.IsWellFormedOriginalString method to verify that URI strings are valid and correct in accordance with the established URI syntax rules outlined in RFC 3986 . Developers can use its functionality to quickly and effectively validate user-provided or dynamically generated URI strings before using them for parsing, storing, or making network requests. Applications can handle errors and validation logic more efficiently with this method since it returns a Boolean value that indicates whether the URI string is well-formed or not. Its usefulness reaches situations in which strict adherence to URI standards is essential for appropriate functionality and security, establishing it as an essential part of URI processing and validation projects in C# applications.