Simon Stender Boisen
Simon Stender Boisen

Reputation: 3431

Uri.IsWellFormedUriString needs to be updated?

I think I might have discovered an error in the Uri.IsWellFormedUriString method, it might be because it only conforms to the RFC 2396 and RFC 2732 standards and not the newer RFC 3986 which makes the two aforementioned obsolete.

What I think happens is that any non us-ascii characters makes it fail, so urls with characters like æ, ø, ö or å in it will make it return false. Since these kind of characters are now allowed (wikipedia among others uses them) I think Uri.IsWellFormedUriString should accept them. The regex below is taken from RFC 3986.

What do you think? Should the Uri class be updated?

Anyway here's some sample code which shows the error:

static void Main(string[] args)
        {
            var urls = new []
                           {
                               @"/aaa/bbb/cccd",
                               @"/aaa/bbb/cccæ",
                               @"/aaa/bbb/cccø",
                               @"/aaa/bbb/cccå"
                           };

            var regex = new Regex(@"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?");

            Debug.WriteLine("");

            foreach (var url in urls)
            {
                if (Uri.IsWellFormedUriString(url, UriKind.Relative))
                    Debug.WriteLine(url + " is a wellformed Uri");

                if (regex.IsMatch(url))
                    Debug.WriteLine(url + " passed the Regex");

                Debug.WriteLine("");
            }
}

Output:

/aaa/bbb/cccd is a wellformed Uri
/aaa/bbb/cccd passed the Regex

/aaa/bbb/cccæ passed the Regex

/aaa/bbb/cccø passed the Regex

/aaa/bbb/cccå passed the Regex

Upvotes: 7

Views: 4709

Answers (1)

Oskar Kjellin
Oskar Kjellin

Reputation: 21900

You have to change you configuration to support the RFC 3986 and RFC 3987. This is the config you have to make:

<configuration>
  <uri>
  <idn enabled="All" />
  <iriParsing enabled="true" />
  </uri>
</configuration>

Taken from here http://msdn.microsoft.com/en-us/library/system.uri.aspx#CodeSnippetContainerCode5

Upvotes: 12

Related Questions