从URL获取主机域?
如何从一个stringURL获得主机域?
GetDomain有1个input“URL”,1个输出“域”
例1
INPUT: http://support.domain.com/default.aspx?id=12345 OUTPUT: support.domain.com 例题
 INPUT: http://www.domain.com/default.aspx?id=12345 OUTPUT: www.domain.com 
示例3
 INPUT: http://localhost/default.aspx?id=12345 OUTPUT: localhost 
	
 您可以使用Request对象或Uri对象来获取url的主机。 
使用Request.Url
 string host = Request.Url.Host; 
使用Uri
 Uri myUri = new Uri("http://www.contoso.com:8080/"); string host = myUri.Host; // host is "www.contoso.com" 
使用Uri类并使用Host属性
 Uri url = new Uri(@"http://support.domain.com/default.aspx?id=12345"); Console.WriteLine(url.Host); 
像这样尝试;
 Uri.GetLeftPart( UriPartial.Authority ) 
定义Uri.GetLeftPart方法的URI部分。
http://www.contoso.com/index.htm?date=today – > http://www.contoso.com
http://www.contoso.com/index.htm#main – > http://www.contoso.com
nntp://news.contoso.com/123456@contoso.com – > nntp://news.contoso.com
file://server/filename.ext – > file:// server
 Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search"); Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Authority)); 
 Demo 
尝试下面的声明
  Uri myuri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri); string pathQuery = myuri.PathAndQuery; string hostName = myuri.ToString().Replace(pathQuery , ""); 
例1
  Input : http://localhost:4366/Default.aspx?id=notlogin Ouput : http://localhost:4366 
例题
  Input : http://support.domain.com/default.aspx?id=12345 Output: support.domain.com 
 最好的方法,正确的方法是使用Uri.Authority字段 
加载和使用Uri像这样:
 Uri NewUri; if (Uri.TryCreate([string with your Url], UriKind.Absolute, out NewUri)) { Console.Writeline(NewUri.Authority); } Input : http://support.domain.com/default.aspx?id=12345 Output : support.domain.com Input : http://www.domain.com/default.aspx?id=12345 output : www.domain.com Input : http://localhost/default.aspx?id=12345 Output : localhost 
如果你想操纵Url,使用Uri对象是做这件事的好方法。 https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx
尝试这个
 Console.WriteLine(GetDomain.GetDomainFromUrl("http://support.domain.com/default.aspx?id=12345")); 
它会输出support.domain.com
或者试试
 Uri.GetLeftPart( UriPartial.Authority ) 
你应该构造你的string作为URI对象和Authority属性返回你所需要的。
WWW是一个别名,所以如果你想要一个域,你不需要它。 这是我的litllte函数从string获取真正的域
 private string GetDomain(string url) { string[] split = url.Split('.'); if (split.Length > 2) return split[split.Length - 2] + "." + split[split.Length - 1]; else return url; }