How do you check if the uri string is correct (which you can pass it to the Uri constructor)?
So far I have only had the following, but for obvious reasons, I would prefer a less crude way:
Boolean IsValidUri(String uri) { try { new Uri(uri); return true; } catch { return false; } }
I tried Uri.IsWellFormedUriString, but it doesn't seem to me that everything you can throw in the constructor. For example:
String test = @"C:\File.txt"; Console.WriteLine("Uri.IsWellFormedUriString says: {0}", Uri.IsWellFormedUriString(test, UriKind.RelativeOrAbsolute)); Console.WriteLine("IsValidUri says: {0}", IsValidUri(test));
The output will be:
Uri.IsWellFormedUriString says: False IsValidUri says: True
Update / Reply
The default Uri constructor uses the good Absolute. This caused a mismatch when I tried to use Uri.TryCreate and the constructor. You will get the expected result if you agree with UriKind for both the constructor and TryCreate.
Thank,
Manuel Jan 29 2018-11-11T00: 00Z
source share