Share via


Extract Server name from URL string.

Question

Monday, July 21, 2014 2:26 AM

Hi,

Which is the best way to extract the server name text from a given URL?

For example
http://forums.asp.net/  I want to get the text "forums.asp" in C#.
http://msdn.microsoft.com/en-us/default.aspx, I want to get the text "msdn.microsoft" in C#.

Can someone suggest the best way to remove the protocol, www, domain such as .com, .net etc and get the name of the website from the URL text?

Thanks

All replies (2)

Monday, July 21, 2014 2:40 AM

Hi Vishnu,

Follow the below post:

http://www.jonasjohn.de/snippets/csharp/extract-domain-name-from-url.htm 

http://stackoverflow.com/questions/14211973/get-host-domain-from-url 


Tuesday, July 22, 2014 1:14 AM

Try using the following method to get the server name :

Request.ServerVariables["SERVER_NAME"]

Note:- "SERVER_NAME" key is used to get the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs.