tags:

views:

145

answers:

3

How to get to know DNS name of the server where ASP.NET application is run?

I want to get string "www.somehost.com" if my application URL is http://www.somehost.com/somepath/application.aspx

Is there some property of Server, Contex, Session or Request objects for this?

Thanks!

A: 

The HTTP_HOST server variable can give you what you need.

Request.ServerVariables("HTTP_HOST")
X-Cubed
That[s the host from the http headers. If there's a reverse-proxy or something in front of the ASP.NET application server, it's probably not the name of the machine that's running the ASP.NET application
Paul
+3  A: 

This will get you the DNS IP for the server that is hosting the web site

void GetDNSServerAddress()
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in nics)
         {
          if (ni.OperationalStatus == OperationalStatus.Up)
          {
           IPAddressCollection ips = ni.GetIPProperties().DnsAddresses;

           foreach (System.Net.IPAddress ip in ips)
           {
            Console.Write(ip.ToString());
           }
          }
         }
    }

However, while writing this ive just seen your edited post, so i think this is what you are after is simply:

string host = Request.Url.Scheme + "://" + Request.Url.Host;

Hope this helps!

hearn
A: 

You can also get the 'machine name' using

System.Environment.MachineName
Dan Esparza
No, it's for NetBIOS name.
Alexander Prokofyev