I am trying to filter out invalid url from valid ones using .NET.
I am using Uri.TryCreate() method for this.
It has the following syntax
public static bool TryCreate(Uri baseUri,string relativeUri,out Uri result)
Now I am doing this….
Uri uri = null;
var domainList = new List<string>();
domainList.Add("asas");
domainList.Add("www.stackoverflow.com");
domainList.Add("www.codera.org");
domainList.Add("www.joker.testtest");
domainList.Add("about.me");
domainList.Add("www.ma.tt");
var correctList = new List<string>();
foreach (var item in domainList)
{
if(Uri.TryCreate(item, UriKind.RelativeOrAbsolute, out uri))
{
correctList.Add(item);
}
}
I am trying the above code I expect it to remove asas and www.joker.testtest from the list, but it doesnt.
Can some one help me out on this.
Update :
just tried out with Uri.IsWellFormedUriString this too did’nt help.
More Update
List of Valid uri
- http://www.ggogle.com
- http://www.abc.com
- http://www.aa.org
- http://www.aas.co
- http://www.hhh.net
- http://www.ma.tt
List of invalid uri
- asas
- as#@SAd
- this.not.valid
- http://www.asa.toptoptop
You seem to be confused about what exactly URL (or URI, the difference is not significant here) is. For example,
http://stackoverflow.comis a valid absolute URL. On the other hand,stackoverflow.comis technically a valid relative URL, but it would refer to the file namedstackoverflow.comin the current directory, not the website with that name. Butstackoverflow.comis a registered domain name.If you want to check whether a domain name is valid, you need to define what exactly do you mean by “valid”:
-). For example,asasandthis.not.validare both valid domain names.asascertainly isn’t an Internet domain name, butthis.not.validcould be.this.not.validwouldn’t be considered valid under this rule, butthisisnotvalid.comwould.http://domaininquestion/) or ping.