How to get the web server full DNS name?

  • Thread starter Thread starter Henri
  • Start date Start date
H

Henri

As my app can bet hosted on different web servers, I need it to retrieve the
web server's name dynamically.
I've tried Dns.HostName with no success.
I want to get the full name (e.g. www.mydomain123.com) and not only the
computer's name.
Maybe there's something to change in IIS, I don't know.
Can you help me?

Thanks

Henri
 
Have you simply tried Request.Url.Host which returns the "the fully
qualified DNS host name or IP address of the server"

Karl
 
Karl,

Request.Url.Host will return the IP address.

The simplest way is to use the ServerVariables
collection and retrieve the HTTP_HOST variable.

Here's a sample page :

servername.aspx
============
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)
Dim dnsname As String = Request.ServerVariables("HTTP_HOST")
Label1.Text = dnsname
End Sub
</script>
<head>
<title>Retrieve Server DNS Name</title>
</head>
<html>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" Runat="server" Text="Label" Width="238px" Height="19px"></asp:Label><br />
</div>
</form>
</body>
</html>
=============

That will return the server's fully qualified DNS name.
You can stuff the dnsname variable anywhere you want to.





Juan T. Llibre
ASP.NET MVP
===========
 
Juan,
I gotta say I disagree with you. I did some quick tests on localhost with my host file and it returned the full domain (www.localhost.com) with www.localhost.com pointing to 127.0.0.1

Additionally, if you look at the documentation it ought to work:

[Visual Basic, C#, JScript] The following example writes the host name (www.contoso.com) of the server to the console.

[Visual Basic]
Dim baseUri As New Uri("http://www.contoso.com:8080/")
Dim myUri As New Uri(baseUri,"shownew.htm?date=today")

Console.WriteLine(myUri.Host)

[C#]
Uri baseUri = new Uri("http://www.contoso.com:8080/");
Uri myUri = new Uri(baseUri, "shownew.htm?date=today");

admittedly I don't have the latest MSDN library though..

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Karl,

Request.Url.Host will return the IP address.

The simplest way is to use the ServerVariables
collection and retrieve the HTTP_HOST variable.

Here's a sample page :

servername.aspx
============
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)
Dim dnsname As String = Request.ServerVariables("HTTP_HOST")
Label1.Text = dnsname
End Sub
</script>
<head>
<title>Retrieve Server DNS Name</title>
</head>
<html>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" Runat="server" Text="Label" Width="238px" Height="19px"></asp:Label><br />
</div>
</form>
</body>
</html>
=============

That will return the server's fully qualified DNS name.
You can stuff the dnsname variable anywhere you want to.





Juan T. Llibre
ASP.NET MVP
===========
 
You're right.

I don't know why, when I ran a short test on
Request.Url.Host, previous to sending my post,
it only returned the IP address.

I just verified both scripts ( http_host and Request.Url.Host )
and they both returned the domain name.

Double your pleasure, double your fun!
Two ways to do it!

Thanks!


Juan T. Llibre
ASP.NET MVP
===========
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message Juan,
I gotta say I disagree with you. I did some quick tests on localhost with
my host file and it returned the full domain (www.localhost.com) with
www.localhost.com pointing to 127.0.0.1

Additionally, if you look at the documentation it ought to work:

[Visual Basic, C#, JScript] The following example writes the host name
(www.contoso.com) of the server to the console.

[Visual Basic]
Dim baseUri As New Uri("http://www.contoso.com:8080/")
Dim myUri As New Uri(baseUri,"shownew.htm?date=today")

Console.WriteLine(myUri.Host)

[C#]
Uri baseUri = new Uri("http://www.contoso.com:8080/");
Uri myUri = new Uri(baseUri, "shownew.htm?date=today");

admittedly I don't have the latest MSDN library though..

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Karl,

Request.Url.Host will return the IP address.

The simplest way is to use the ServerVariables
collection and retrieve the HTTP_HOST variable.

Here's a sample page :

servername.aspx
============
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)
Dim dnsname As String = Request.ServerVariables("HTTP_HOST")
Label1.Text = dnsname
End Sub
</script>
<head>
<title>Retrieve Server DNS Name</title>
</head>
<html>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" Runat="server" Text="Label" Width="238px"
Height="19px"></asp:Label><br />
</div>
</form>
</body>
</html>
=============

That will return the server's fully qualified DNS name.
You can stuff the dnsname variable anywhere you want to.





Juan T. Llibre
ASP.NET MVP
===========
 
Back
Top