With so many different endings for domain names, it's hard to know if they're valid or not. This project uses a list from publicsuffix.org, which keeps track of all the common endings like .com
or .org
. It checks domain names against this list to see if they're okay. Then, it splits the domain into three parts: the ending (like .com), the main part (like google), and any subparts (like www). You can find the list on GitHub under publicsuffix list repository.
If you like the new version then give the project a :star: or become a sponsor
.NET8
IRuleProvider
HttpClient
for CachedHttpRuleProvider
TLD
domains improvedDomainInfo->Hostname
renamed to DomainInfo->FullyQualifiedDomainName
The source code of version 2 can be found here
Fully Qualified Domain Name (FQDN) | Top Level Domain (TLD) | Domain | Subdomain |
---|---|---|---|
blog.google.com | com | blog | |
22.cn | cn | 22 | |
www.volkswagen.de | de | volkswagen | www |
www.amazon.co.uk | co.uk | amazon | www |
www.wikipedia.org | org | wikipedia | www |
The package is available on nuget
PM> install-package Nager.PublicSuffix
var ruleProvider = new LocalFileRuleProvider("public_suffix_list.dat");
await ruleProvider.BuildAsync();
var domainParser = new DomainParser(ruleProvider);
var domainInfo = domainParser.Parse("sub.test.co.uk");
//domainInfo.Domain = "test";
//domainInfo.FullyQualifiedDomainName = "sub.test.co.uk";
//domainInfo.RegistrableDomain = "test.co.uk";
//domainInfo.Subdomain = "sub";
//domainInfo.TopLevelDomain = "co.uk";
var ruleProvider = new LocalFileRuleProvider("public_suffix_list.dat");
await ruleProvider.BuildAsync();
var domainParser = new DomainParser(ruleProvider);
var isValid = domainParser.IsValidDomain("sub.test.co.uk");
// after -> var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient(); //Required for CachedHttpRuleProvider
builder.Services.AddSingleton<ICacheProvider, LocalFileSystemCacheProvider>();
builder.Services.AddSingleton<IRuleProvider, CachedHttpRuleProvider>();
builder.Services.AddSingleton<IDomainParser, DomainParser>();
// after -> var app = builder.Build();
var ruleProvider = app.Services.GetService<IRuleProvider>();
if (ruleProvider != null)
{
await ruleProvider.BuildAsync();
}
// minimal api
app.MapGet("/DomainInfo/{domain}", (string domain, IDomainParser domainParser) =>
{
domain = HttpUtility.UrlEncode(domain);
var domainInfo = domainParser.Parse(domain);
return domainInfo;
})
.WithName("DomainInfo")
.WithOpenApi();
var httpClient = new HttpClient();
var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider();
var ruleProvider = new CachedHttpRuleProvider(cacheProvider, httpClient);
await ruleProvider.BuildAsync();
var domainParser = new DomainParser(ruleProvider);
var domainInfo = domainParser.Parse("example.com");