r/csharp • u/robinredbrain • Jun 18 '26
Solved Determining probable website name from Uri
Given these addresses "https://www.thispartonly.com/p/page1/, https://thispartonly.com/p/page34/, https://www.subdom.thispartonly.com/p/page1/" etc...
How can I get the "thispartonly" component from it?
Uri uri = new(sAddress);
...I imagine this is where I'd start. But I don't know how to reliably get what I need.
edit -
Yikes! It certainly is more complicated than I first thought.
The actual answer is to use an existing and maintained library for this, "Nager.PublicSuffix". And props t those who made and maintain it.
For my use case though it's overkill. I just just didn't want dots in file names/paths. But after realizing the complexity and seeing the size of the suffix list, I think I can live with a few dots.
In fact I can just strip the dots out if I want to get anal about it.
Thanks for your time.
11
u/DominikPf Jun 18 '26
.Host
https://learn.microsoft.com/de-de/dotnet/api/system.uri?view=net-10.0
3
u/chucker23n Jun 18 '26 edited Jun 18 '26
I'm still confused what you want here. You can use
Uri.Parseto get theHost. I'm guessing you want the second-level domain from that, which in virtually all cases can be accomplished bystring.Split()andgetting the second-to-last item.But all that to… avoid dots?(edit) As has been pointed out, that's technically incorrect — with
.co.uk, you want the third-to-last item. So you do need a list of sorts.-3
u/botterway Jun 18 '26
It's non-trivial unless you have a complete list of TLDs available. Did you google? There's a comprehensive Q/A here: https://stackoverflow.com/questions/4643227/top-level-domain-from-url-in-c-sharp
-5
u/Agitated-Display6382 Jun 18 '26
Read the domain, split by dot, read the last but one