How to get server / website IP address in asp.net?

When a user request arrives, I can use Context.Request.UserHostAddress to get the IP address of the user. How can I get the website / server IP address at runtime? I have a reporting code that can be used by several websites on the same server, and each website uses a different IP address. Therefore, I need to be able to detect the IP address of the website at runtime.

+7
source share
3 answers

Thanks Alex, your answer set me on the right track. Here is the code to do:

VB.NET:

System.Net.Dns.GetHostAddresses(Request.Url.Host)(0).ToString() 

or

 System.Net.Dns.GetHostEntry(Request.Url.Host).AddressList(0).ToString() 
+6
source share

System.Net.Dns.GetHostAddresses

By the way, you should pass the hostname as an argument, so maybe try the following:

 System.Net.Dns.GetHostByAddress(System.Net.IPAddress.Parse(System.Web.HttpContext.Current.Request.UserHostName)).HostName; 

And if all else fails, just do it the old school way:

 Response.Write(Request.ServerVariables["LOCAL_ADDR"]); 
+14
source share
  string siteName = "Your Site URL"; string tempUrl = siteName.Replace("http://", "").Replace("https://", "").Trim(); string[] SiteURLArr = tempUrl.Split('/'); string SiteURL = SiteURLArr[0]; System.Net.IPAddress[] ip = System.Net.Dns.GetHostAddresses(SiteURL); Response.Write(ip[0]); 
+2
source share

All Articles