Last post Nov 26, 2018 01:37 AM by bruce (sqlwork.com)
Nov 20, 2018 03:03 PM|InspiredJide|LINK
I trying to write a function that can detect a url and different a sub domain from a regular domain.
Been trying several codes but yet to get the best version.
Nov 20, 2018 04:15 PM|PatriceSc|LINK
My understanding is that you are trying to read DNS records as shown at https://www.codeproject.com/Articles/23673/DNS-NET-Resolver-C (that you may have tried already ?).
It could be best to start from the better result you got so far and explaining what you are looking for rather than starting from 0 again. At first sight I though first you wanted to parse a url (net is a top level domain, with asp being a subdomain) but
surely you want to dig into DNS records for some reason ?
Nov 20, 2018 04:40 PM|bruce (sqlwork.com)|LINK
its really pretty simple. a url is a tree built based on the dots
for example: a.b.c.com
. -> com -> c -> b -> a
the first node (.) is name less
the second node (com) is the top level domain and the names are predefined (com, edu, fr, etc)
the third node (c) is the root domain (this is what is registered)
the fourth and beyond are sub domains
generally if you are the register owner of a domain, you can add sub domain records.
Nov 25, 2018 07:15 PM|InspiredJide|LINK
Thanks for your response. You made a good point here. So I already wrote a code that I think can work. I would paste it and you let me know if it is standard enough.
This method is suppose to tell me when a subdomain url is parsed from the browser.
public bool Checkifsubdomain()
var requestDomain = HttpContext.Request.Url.Host;
requestDomain = requestDomain.Replace("http://", "");
requestDomain = requestDomain.Replace("https://", "");
requestDomain = requestDomain.Replace("www.", "");
requestDomain = requestDomain.Replace(".com","");
bool issubdomain = false;
requestDomain = requestDomain.ToLower();
if (requestDomain.Contains(".")) //Since we have removed all the other dots. The domain string would only contact dot if it is a subdomain
issubdomain = true;
Nov 26, 2018 01:37 AM|bruce (sqlwork.com)|LINK
i'd use the Uri class:
var uri = new Uri(url);
var domain = "";
var subDomain = "";
if (uri.HostNameType == UriHostNameType.Dns)
var parts = uri.Host.Split('.');
if (parts.Length > 1)
domain = string.Join(".",parts.ToList().Skip(parts.Length - 2));
if (parts.Length > 2)
subDomain = string.Join(".", parts.ToList().Take(parts.Length-2));
Console.WriteLine("Domain: " + domain);
Console.WriteLine("Subdomain: " + subDomain);