The TLD proliferation makes it difficult to check whether domain names are valid. This project uses the rules of publicsuffix.org, a list of known public domain suffixes (TLD) to validate and split domains into three the parts (TLD, domain, subdomain). The validation rules are loaded directly from https://publicsuffix.org.
A domain name has 3 major parts:
Example | TLD | Domain | Subdomain |
---|---|---|---|
blog.google.com | com | blog | |
www.wikipedia.org | org | wikipedia | www |
mail.yandex.ru | ru | yandex | |
www.amazon.co.uk | co.uk | amazon | www |
The package is available on nuget
PM> install-package Nager.PublicSuffix
- High performance
- FileTldRuleProvider or WebTldRuleProvider
- CacheProvider
- Async support
Without a custom config the WebTldRuleProvider
has a default cache live time of 1 day, then you must refresh the cache with execute BuildAsync
;
var domainParser = new DomainParser(new WebTldRuleProvider());
var domainInfo = domainParser.Parse("sub.test.co.uk");
//domainInfo.Domain = "test";
//domainInfo.Hostname = "sub.test.co.uk";
//domainInfo.RegistrableDomain = "test.co.uk";
//domainInfo.SubDomain = "sub";
//domainInfo.TLD = "co.uk";
Without a custom config the WebTldRuleProvider
has a default cache live time of 1 day, then you must refresh the cache with execute BuildAsync
;
var domainParser = new DomainParser(new WebTldRuleProvider());
var isValid = domainParser.IsValidDomain("sub.test.co.uk");
//cache data for 10 hours
var cacheProvider = new FileCacheProvider(cacheTimeToLive: new TimeSpan(10, 0, 0));
var webTldRuleProvider = new WebTldRuleProvider(cacheProvider: cacheProvider);
var domainParser = new DomainParser(webTldRuleProvider);
for (var i = 0; i < 100; i++)
{
var isValid = webTldRuleProvider.CacheProvider.IsCacheValid();
if (!isValid)
{
webTldRuleProvider.BuildAsync().GetAwaiter().GetResult(); //Reload data
}
var domainInfo = domainParser.Parse($"sub{i}.test.co.uk");
}
var domainParser = new DomainParser(new FileTldRuleProvider("effective_tld_names.dat"));
var domainInfo = domainParser.Parse("sub.test.co.uk");
//domainInfo.Domain = "test";
//domainInfo.Hostname = "sub.test.co.uk";
//domainInfo.RegistrableDomain = "test.co.uk";
//domainInfo.SubDomain = "sub";
//domainInfo.TLD = "co.uk";