0

I am trying to implement uri validation as follows.

For the sake of testing, I assigned my url as an empty string "", and tested the following code, but it returns me True.

I wonder what I am doing wrong/missing?

var a = "";
if (Uri.IsWellFormedUriString(a, UriKind.RelativeOrAbsolute))
{
     Debug.WriteLine("True");

 }
 else
 {
     Debug.WriteLine("Error");
 }
1
  • 1
    Why are you calling ToString on a string? Commented Jan 9, 2017 at 21:04

2 Answers 2

1

See what RFC2396 says about URIs:

4.2. Same-document References

A URI reference that does not contain a URI is a reference to the
current document. In other words, an empty URI reference within a
document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference
to the identified fragment of that document. Traversal of such a
reference should not result in an additional retrieval action.
However, if the URI reference occurs in a context that is always
intended to result in a new request, as in the case of HTML's FORM
element, then an empty URI reference represents the base URI of the
current document and should be replaced by that URI when transformed
into a request.

Casually, see what MSDN says:

The string is considered to be well-formed in accordance with RFC 2396 and RFC 2732 by default. If International Resource Identifiers (IRIs) or Internationalized Domain Name (IDN) parsing is enabled, the string is considered to be well-formed in accordance with RFC 3986 and RFC 3987

So how to handle the empty URI scenario?

The answer is up to you. While an empty URI can be considered as valid, maybe in your scenario isn't valid. Thus, you need to add a condition in your if statement:

if (!string.IsNullOrEmpty(a) && Uri.IsWellFormedUriString(a, UriKind.RelativeOrAbsolute))
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure if this is good method for uri validation. But i can be wrong of course. Where is that problem ? I found this "en.wikipedia.org/wiki/Uniform_Resource_Identifier" and there is an sample for uri "ldap://[2001:db8::7]/c=GB?objectClass?one" but with method mentioned above is marked as invalid uri.
@msavara hey, I'm not sure if the query string is valid in that case... or who knows if that method can handle this URI format based on supported RFC standards.
1

Empty string is a valid relative URI.

4 Comments

How would I handle then? I could add empty string checker, but I wonder Is there any other strings are considered as a valid URI ?
@hotspring Handle what? There's nothing to handle; it's a valid URI.
It really depends on what you really want and why you would like to test whether a URI is valid. If your program does not consider empty string legal, then you can surely explicitly add a test (like the one in @MatiasFidemraizer's post) to eliminate the empty string case. But since empty URI is valid, why would you do that?
Another case that you might consider empty string illegal is that you only accept absolute URI. Is that what you want? If so, you should specify UrlKind.Absolute in IsWellFormedUriString.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.