locked
writing a function that can detect subdomains from regular domains. RRS feed

  • Question

  • User-1204637165 posted

    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. 

    Thanks alot.

    Tuesday, November 20, 2018 3:03 PM

Answers

  • User-1204637165 posted

    Dear Bruce,

    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.

     [NonAction]
            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;
                }
    
                return issubdomain;
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 25, 2018 7:15 PM
  • User-474980206 posted

    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);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 26, 2018 1:37 AM

All replies

  • User753101303 posted

    Hi,

    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 ?

    Tuesday, November 20, 2018 4:15 PM
  • User-474980206 posted

    its really pretty simple. a url is a tree built based on the dots

    for example:  a.b.c.com

    becomes

       . -> 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.

    Tuesday, November 20, 2018 4:40 PM
  • User-1204637165 posted

    Dear Bruce,

    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.

     [NonAction]
            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;
                }
    
                return issubdomain;
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 25, 2018 7:15 PM
  • User-474980206 posted

    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);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 26, 2018 1:37 AM