I am having a requirement to correctly get the domain/subdomain based on the current url. this is required in order to correctly fetch the data from database and further call web api with correct parameters.
In perticular, I am facing issues with local and production urls. for ex.
In local, i have
http://sample.local.example.com
http://test.dev.example.com
In production, i have
http://client.example.com
http://program.live.example.com
i need
Subdomain as: sample / test / client / program
Domain as: exmpale
So far i tried to use c# with following code to identify the same. It works fine on my local but i am sure this will create an issue on production at some point of time. Basically, for Subdomain, get the first part and for Domain, get the last part before ''.com''
var host = Request.Url.Host;
var domains = host.Split('.');
var subDomain = domains[0];
string mainDomain = string.Empty;
#if DEBUG
mainDomain = domains[2];
#else
mainDomain = domains[1];
#endif
return Tuple.Create(mainDomain, subDomain);