Javatpoint Logo
Javatpoint Logo

Uri.IsWellFormedOriginalString() method in C#

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:

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:

Example 1:

Let us take an example to illustrate the Uri.IsWellFormedOriginalString() method in C#.

Output:

The URI is well-formed.

Example 2:

Let us take another example to illustrate the Uri.IsWellFormedOriginalString() method in C#.

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#.

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.


Next TopicValueTuple in C#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA