I need to check whether the current URL has the subdomain name and if it has the subdomain, I want extract it from the URL.
Could anyone konw how to do this in the C# in an ASP.NET MVC app.
Thanks.
I need to check whether the current URL has the subdomain name and if it has the subdomain, I want extract it from the URL.
Could anyone konw how to do this in the C# in an ASP.NET MVC app.
Thanks.
Subdomain in what sense exactly?
You could do:
Uri u = new Uri("http://sub.mydomain.com/path/file.htm"); if(i.Host == "sub.mydomain.com") { ... }
Is that what you want?
Is this what you're looking for?
Uri fullPath = new Uri("http://subdomain.domain.topleveldomain/index.html");
string hostname = fullPath.Host; // returns "subdomain.domain.topleveldomain"
char[] separators = new char[]{'.'};
// returns {"subdomain","domain","topleveldomain"}
string[] domains = hostname.Split(separators);
string subdomain = domains[0];
string domain = domains[1];
string tld = domains[2];
Note that you'll need to change it a bit if it's possible there will be more than one subdomain, I.E. http://subsub.sub.dom.tld/
or something like that.