tags:

views:

1739

answers:

2

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

+7  A: 

System.Net.Dns.GetHostAddresses

by the way, you must pass in as an argument the name of the host, so perhaps try this:

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"]);
alex
+2  A: 

Thanks Alex, your answer put me on the right path. Here is the code to do what I am looking for:

VB.NET:

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

or

System.Net.Dns.GetHostEntry(Request.Url.Host).AddressList(0).ToString()
ccoxtn
This doesn't work if your host is behind a content switch. What would you need to do to find the IP address on which the local server's site is running in IIS if you're behind a content switch?
alord1689