C#: Bug in Uri.IsWellFormedUriString?!

A few days ago I had to analyze a string, figure out whether it is a URI and if yes, then do something with parts of the URI, like the host, the pathname and the parameters. Since I was coding in C# I thought the Uri class would be just ideal for that.

So first I implemented this

Uri myUri;
try {
    myUri = new Uri(myUrlString);
} catch (UriFormatException) {
    // do something
}

Then however I realized that there is a function for testing the validity of a string:

Uri myUri;
if (Uri.IsWellFormedUriString(myUrlString)) {
    myUri = new Uri(myUrlString);
}

This is much nicer. I don’t like using exceptions to handle expected workflows. In this case the string can be a valid or invalid Uri, so really the if-then-else construction is nicer than the try-catch. The problem is that even though IsWellFormedUriString returns true you might not be able to create an Uri instance from it. Consider this small programm:

using System;
namespace DemoConsoleApp
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			Uri myUri;
 
			string myUrl1 = "www.mydomain.de/test/file.aspx";
			string myUrl2 = "http://www.mydomain.de/test/file.aspx";
 
			Console.WriteLine(Uri.IsWellFormedUriString(myUrl1, UriKind.RelativeOrAbsolute));
			Console.WriteLine(Uri.IsWellFormedUriString(myUrl2 ,UriKind.RelativeOrAbsolute));
 
			try {
				myUri = new Uri(myUrl1);
			} catch (UriFormatException) {
				Console.WriteLine("Failed to create Uri from myUrl1");
			}
 
			try {
				myUri = new Uri(myUrl2);
			} catch (UriFormatException) {
				Console.WriteLine("Failed to create Uri from myUrl2");
			}
		}
	}
}

The output is :

True
True
Failed to create Uri from myUrl1

According to the documentation, the first call of Uri.IsWellFormedUriString should returns false. If it did this would be also consistent with the constructor, which fails for myUrl1.

By default, the string is considered well-formed in accordance with RFC 2396 and RFC 2732. If International Resource Identifiers (IRIs) or Internationalized Domain Name (IDN) parsing is enabled, the string is considered well-formed in accordance with RFC 3986 and RFC 3987.
The string is considered poorly formed, causing the method to return false, if any of the following conditions occur
[...]
The string represents a hierarchical absolute Uri and does not contain “://” – www.contoso.com/path/file

DeliciousTwitterFacebookLinkedInRedditSlashdotTechnorati FavoritesDiggShare
This entry was posted in C#. Bookmark the permalink.

One Response to C#: Bug in Uri.IsWellFormedUriString?!

  1. Dave says:


    The problem is that you are using two different conditions – one in the test, and the other in the constructor.

    The condition in the constructor is implied, but still there. The constructor you are using is the same as new Uri( myUrl1, UriKind.Absolute ) and fails because the Url is not absolute.

    If you change the constructor to match the test: new Url( myUrl1, UriKind.RelativeOrAbsolute ), then the constructor will succeed, as well.

    Alternatively, changing the flag in the test to use UriKind.Absolute will cause the test to fail, matching the response of the constructor.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">