How to get the web server full DNS name?

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
 
K

Karl Seguin

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

Karl
 
J

Juan T. Llibre

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
===========
 
K

Karl Seguin

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
===========
 
J

Juan T. Llibre

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
===========
 
Joined
Jul 6, 2006
Messages
4
Reaction score
0
getting the full dns name or full domain name

i also tried a lot for this problem and finally we solved this.
to get the full name of the domain we can use the following piece of code.
page.request.servervariable["http_host"]

Note:
if we run this code in our machine, we will always get the output as "localhost".
so better put the code in client machine or in production server where it surely works.
u will get the output something like"support.oracle.com/survey/login.html"
Bedt wishes to achieve success

Thanks & Regards
Balaji


Henri said:
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
 
Joined
Nov 28, 2008
Messages
3
Reaction score
0
Embedding images into HTML to word converted document.

Thank you very much for this post it helped me to solve embedding images into HTML to word converted document.

Using Server.MapPath() does not work in this case, you have to specify the http url of the folder that contains the images.

I got "page.request.servervariable["http_host"]" which helped me to get the url of the server I hosted my application. With this I then put little codes together to get the full url of my image folder.

See the codes below.

string url = Page.Request.ServerVariables["http_host"];

string image_folder = "http:" + url + "/project folder name/app_images/";
"Note: please put // in front of http:, I do this because the web site says I can't post links or images because my posts are not up to 25"

"<div><img src='" + image_folder +ds.Tables[0].Rows[0]["database_field that contains the name of the image file"] + "'><div>";

With this I was able to embed the image into my word file.

Thank you for this forum.

Bioscom


itskbalaji said:
i also tried a lot for this problem and finally we solved this.
to get the full name of the domain we can use the following piece of code.
page.request.servervariable["http_host"]

Note:
if we run this code in our machine, we will always get the output as "localhost".
so better put the code in client machine or in production server where it surely works.
u will get the output something like"support.oracle.com/survey/login.html"
Bedt wishes to achieve success

Thanks & Regards
Balaji
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top