Get host domain from URL?

how to get host domain from string url?

GetDomain has 1 input "URL", 1 output "Domain"

Example 1

INPUT: http://support.domain.com/default.aspx?id=12345 OUTPUT: support.domain.com 

Example 2

 INPUT: http://www.domain.com/default.aspx?id=12345 OUTPUT: www.domain.com 

Example 3

 INPUT: http://localhost/default.aspx?id=12345 OUTPUT: localhost 
+80
string c # url uri
Jan 08 '13 at 9:36
source share
8 answers

You can use the Request or Uri object to get the URL node.

Using Request.Url

 string host = Request.Url.Host; 

Using Uri

 Uri myUri = new Uri("http://www.contoso.com:8080/"); string host = myUri.Host; // host is "www.contoso.com" 
+162
Jan 08 '13 at 9:37
source share

Use the Uri class and use the Host property

 Uri url = new Uri(@"http://support.domain.com/default.aspx?id=12345"); Console.WriteLine(url.Host); 
+22
Jan 08 '13 at 9:39
source share

Try it:

 Uri.GetLeftPart( UriPartial.Authority ) 

Defines the URI parts for the Uri.GetLeftPart method.




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

+21
Jan 08 '13 at 9:41
source share

try the following instruction

  Uri myuri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri); string pathQuery = myuri.PathAndQuery; string hostName = myuri.ToString().Replace(pathQuery , ""); 

Example 1

  Input : http://localhost:4366/Default.aspx?id=notlogin Ouput : http://localhost:4366 

Example 2

  Input : http://support.domain.com/default.aspx?id=12345 Output: support.domain.com 
+3
Jul 12 '14 at 5:59
source share

The best way and the right way to do this is to use the Uri.Authority field

Download and use Uri like this:

 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 

If you want to control Url, using a Uri object is a good way to do this. https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx

+2
Mar 27 '15 at 16:33
source share

try it

 Console.WriteLine(GetDomain.GetDomainFromUrl("http://support.domain.com/default.aspx?id=12345")); 

It will print support.domain.com

Or try

 Uri.GetLeftPart( UriPartial.Authority ) 
0
Jan 08 '13 at 9:37
source share

You should build your string as a URI and Authority returns what you need.

0
Jan 08 '13 at 9:42
source share

WWW is an alias, so you do not need it if you want a domain. Here is my litllte function to get a real domain from a 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; } 
-one
Mar 27 '15 at 16:20
source share



All Articles