Development Desk

Just another site

Get Domain Name without any Sub-domain name


you can get domain name without any sub-domain by using below method

public string GetDom(HttpRequest Request)
          var fullDomain = new      Uri(Request.Url.Host.ToLower()).GetComponents(UriComponents.Host, UriFormat.SafeUnescaped);

        if (fullDomain.EndsWith(“.com”))
               return GetHostUrl(fullDomain, 2);
        else if (fullDomain.EndsWith(“”))
               return GetHostUrl(fullDomain, 3);
        return fullDomain;

public string GetHostUrl(string hostUrl, int parameters)
         var domainParts = hostUrl
         .Split(‘.’) // [“test”, “example”, “co”,”uk”]
         .Reverse() // [“uk”,”co”, “example”, “test”]
         .Take(parameters) // [“uk”,”co” “example”]
         .Reverse(); // [“example”, “co”,”uk”]
          var domain = String.Join(“.”, domainParts);
          return domain;

These method can be made more generic.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: