Quantcast
750×100
Read WebProNews
With Friends!

Retrieve Subdomain from a URL in C#

Get the WebProNews Newsletter:

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 Mads Kristensen
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/
Top Rated White Papers and Resources
There are 6 Comments. Add Yours.
  1. 0 0
    ravi

    private string GetSubDomain(Uri url)
    {
    if (url.HostNameType == UriHostNameType.Dns)
    {
    string host = url.Host;
    if (host.Split(‘.’).Length > 2)
    {
    int firstIndex = host.IndexOf(“.”);
    string subdomain = host.Substring(0, firstIndex);
    return subdomain;
    }
    }

    return string.Empty;
    }

    Reply
  2. 0 0
    Guest

    this is help ful for me

    Reply
  3. 0 0
    .Net Monkey

    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;
    }

    Reply
  4. 0 0
    Guest

    Handy dandy.  Thanks.

    Reply

What do you think? Respond.

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>