001 001 - 1 year ago 152
C# Question

Get host domain from URL?

how to get host domain from a string URL?

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


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

Answer Source

You can use Request object or Uri object to get host of url.

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download