Monday, July 19, 2010

How to Retrieve Subdomain from URI

Hi,

Herewith, I have given below the code to retrieve the uri.

Uri uri = new Uri("http://t1.testing.com/Web1.aspx?Id=5");

public static string RetrieveSubDomain(Uri url)
{
string subDomain = "";
if (url.HostNameType == UriHostNameType.Dns && (!(url.HostNameType == UriHostNameType.Unknown)))
{
string host = url.Host;
int length = host.Split('.').Length;
if (length > 2)
{
int lastIndex = host.LastIndexOf(".");
int index = host.LastIndexOf(".", lastIndex - 1);
subDomain = host.Substring(0, index);
}
}

return subDomain.ToLower();
}

No comments: