Retrieves the subdomain from the specified URL.
using System;
using System.IO;
using System.Net.Mail;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Globalization;
using System.Web;
using System.Web.Configuration;
using System.Threading;
using System.Reflection;
using System.Collections;
using System.Xml;
using System.Net;
using System.Web.Caching;
namespace BlogEngine.Core
{
/// <summary>
/// Utilities for the entire solution to use.
/// </summary>
public static class Utils
{
/// 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>
public 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;
}
}
}
Related examples in the same category