Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the server/website IP address in asp.net?

Tags:

asp.net

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.

like image 949
ccoxtn Avatar asked Jan 08 '09 14:01

ccoxtn


2 Answers

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"]);
like image 55
alex Avatar answered Oct 19 '22 10:10

alex


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()
like image 45
ccoxtn Avatar answered Oct 19 '22 11:10

ccoxtn