If the string utilized to generate this Uri was correctly structured and doesn't require additional escaping, its well-formed status can be verified through the Uri.IsWellFormedOriginalString function. The C# Uri class enables the manipulation of URIs (Uniform Resource Identifiers), offering essential functionalities like the Uri.IsWellFormedOriginalString method. This particular method serves the purpose of validating whether a URI string adheres to the established syntax regulations defined in RFC 3986.
Syntax:
It has the following syntax:
public bool IsWellFormedOriginalString ();
The function will return either false or true based on the correctness of the string's format.
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's consider a scenario to demonstrate the Uri.IsWellFormedOriginalString function 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's consider another scenario to demonstrate 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's consider another instance to demonstrate the Uri.IsWellFormedOriginalString function 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:
To summarize, the C# Uri.IsWellFormedOriginalString method is a valuable tool for ensuring the accuracy and validity of URI strings based on the defined rules of URI syntax specified in RFC 3986. This method enables developers to efficiently validate user-provided or dynamically generated URI strings before utilizing them for various operations such as parsing, storage, or network communication. By providing a Boolean output indicating the well-formedness of the URI string, developers can enhance error handling and validation processes within their applications. This method proves particularly beneficial in scenarios where strict adherence to URI standards is crucial for maintaining functionality and security, making it an indispensable component of URI processing and validation tasks within C# projects.