iEntry 10th Anniversary RSS Newsletter Advertising
Visit Twellow.com

Retrieve Subdomain from a URL in C#

Post to Twitter Post to Facebook

I had to come up with a method that retrieved the subdomain from the current web request on an ASP.NET website.

I thought that the System.Uri class contained that information in an easy retrievable way, but no.

Here's what I came up with instead. It still uses the System.Uri to find the subdomain.

/// <summary>
/// Retrieves the subdomain from the specified URL.
/// </summary>
/// <param name="url">The URL from which to retrieve the subdomain.</param>
/// <returns>The subdomain if it exist, otherwise null.</returns>
private static string GetSubDomain(Uri url)
{
   string host = url.Host;
   if (host.Split('.').Length > 1)
   {
     int index = host.IndexOf(".");
     return host.Substring(0, index);
   }

   return null;
}

To use it, simply put in the URL of your choice like so:

GetSubDomain(Request.Url);

Update

Based on the comments I've had so far on this post, I've made some changes to the GetSubDomain() method as shown below.

Thanks to Anders and Jacob for the comments.

/// Retrieves the subdomain from the specified URL.
/// </summary>
/// <param name="url">The URL from which to retrieve the subdomain.</param>
/// <returns>The subdomain if it exist, otherwise null.</returns>
private static string GetSubDomain(Uri url)
{
   if (url.HostNameType == UriHostNameType.Dns)
   {
     string host = url.Host;
     if (host.Split('.').Length > 2)
     {
       int lastIndex = host.LastIndexOf(".");
       int index = host.LastIndexOf(".", lastIndex - 1);
       return host.Substring(0, index);
     }
   }

     return null;
}

Comments

Tag:

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About the author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

3 Comments

What about this then...

public static string GetSubDomain(Uri url)
{
if (url.HostNameType == UriHostNameType.Dns)
{
string host = url.Host;
if (host.Split('.').Length > 2)
{
int index = host.IndexOf(".");
return host.Substring(0, (index - 1));
}
}

return null;
}

this code is crap! what

this code is crap! what about www.example.co.uk ???

Handy dandy.  Thanks.

Handy dandy.  Thanks.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
3 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Featured Headline
Fake Chrome OS Screenshots Punk Tech Media
Mystery Blogger Comes Clean
5 comments | 16 hours ago
 
Subscribe to WebProNews


Send me relevant info