Tuesday, March 1, 2011

DNS of the server where ASP.NET application is run

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!

From stackoverflow
  • The HTTP_HOST server variable can give you what you need.

    Request.ServerVariables("HTTP_HOST")
    
    Paul : 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
  • 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!

  • You can also get the 'machine name' using

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

0 comments:

Post a Comment