Thursday, March 05, 2009

Get the Host name and IP address from ASP.NET

To get the Host name and IP address from your ASP.NET application, we have option is .NET. Using the using System.Net; namespace you have access to the classes which can expose the Host name and IP address.

The sample code to explain the solution. Paste the same code in the page load event of the ASP.NET page.

using System.Net;
//Get the Host name
string strHostName = string.Empty;
strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
//Get the IP address
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Response.Write("IP Address {0}: {1} " + i + addr[i].ToString() + ", HostName: " + strHostName);
}

2 comments:

Saif ur Rehman Saifi said...

Dear,
Thank you very much, i was stuck with this problem, i have found it and it is working fine,

thanks

Saif ur rehman saifi
Islamabad, Pakistan

Unknown said...

Dear, Thanks For This easy and useful code, but this code to get the host info. but how to get the Client ip and computer name to use it in MaintenanceRequest.

Thanks

ASP.NET MVC - Sport Facility Booking system

  The project relies on some external service providers. Thus accessing them via their API requires authentication. An API Key need to be su...