Switching Domain name depending on server?

B

brett

I use two test servers and one production. I have a Utilities class
that holds many of the links used through the site. One of the
variables in this class is assigned the domain name. I need to
automatically set this value depending on the server I'm on. The
domain can be similar to any of the following depending on the server:

- abc.com
- 190.168.2.100
- 190.168.2.109

What is the best way to do that?

Thanks,
Brett
 
B

Brennan Stehling

Brett,

Why not use the hosts file? It is located here...

C:\WINDOWS\system32\drivers\etc\hosts

And it would look like this.

127.0.0.1 localhost

192.168.11.11 dbserver1
192.168.11.12 dbserver2

192.168.11.21 webserver1
192.168.11.22 webserver2

These hostnames point to IP addresses and you could place the same
hosts file contents on each server. But you can also use the same set
of names with different IP addresses. You Staging and Testing servers
could point to Staging and Testing database servers and have the
settings in your application simply refer to the hostname you place in
the hosts file.

When you move the identical application to your Production server the
application will use the same names but connect to the alternate IP
addresses. This is a good approach because the developers do not have
to be concerned with changes on the servers and instead just code
against a constant name.

Alternatively you could change the values in the appSettings of your
Web.config/App.config during deployment. If you are running many
applications that means copying the deployment details to each
application and updating each application when a server/network change
is made.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
P

Patrice

Do you really need the domain name in those links ? If you use root relative
links (i.e /public/mypage.aspx or ~/public/mypage.aspx), you refer to the
server from which the page that contains this link is served...
 
B

brett

Brennan said:
Brett,

Why not use the hosts file? It is located here...

C:\WINDOWS\system32\drivers\etc\hosts

And it would look like this.

127.0.0.1 localhost

192.168.11.11 dbserver1
192.168.11.12 dbserver2

192.168.11.21 webserver1
192.168.11.22 webserver2

You have all different names so I'm not sure how that helps.

Do you mean I get use this on each different machine:

machine1
192.168.11.21 mydomain

machine2
192.168.11.22 mydomain

machine3
192.168.11.23 mydomain

and in the code just use "mydomain" instead of localhost or an IP?

Thanks,
Brett
 
B

brett

Patrice said:
Do you really need the domain name in those links ? If you use root relative
links (i.e /public/mypage.aspx or ~/public/mypage.aspx), you refer to the
server from which the page that contains this link is served...

Using your suggestion, say I create a static variable:

public static LinkOne = "test/testone.aspx";

The above page is actually in root/test/testone.aspx

I have these two pages:

root/mydir/somepage.aspx
root/somedir/subdir/anotherpage.aspx

If I stick the LinkOne variable into any of the above pages, it won't
work. Both pages will look for test/testone.aspx in their respective
directories. But testone.aspx is in the root. That's the problem,
when pages are located in varied directory structures, how do you use a
relative path in a variable?

Brett
 
B

Brennan Stehling

I thought the question was about connecting with database and web
services on other hosts. If a relative path to pages within the same
site is all you need, you can use the utility methods I use.

http://svn.offwhite.net/svn/SmallSharpTools.UrlMapper/trunk/Website/App_Code/Utility.cs

I discovered these methods in the Commerce Starter Kit originally and
have adjusted them to suite my needs. You just provide a path like
this...

~/someDirectory/page.aspx

It will change the tilde (~) to the root of the current website.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
P

Patrice

This link is relative to the current location. Instead use public static
LinkOne = "/root/test/testone.aspx"; or public static LinkOne =
"~/root/test/testone.aspx"; (note the leading / or ~/) that is a location
that is relative to the web site root or to the web application root (note
that ~ is processed server side i.e. it will work if processed by an ASP.NET
control or explicitely resolved).
 
B

brett

Patrice said:
This link is relative to the current location. Instead use public static
LinkOne = "/root/test/testone.aspx"; or public static LinkOne =
"~/root/test/testone.aspx"; (note the leading / or ~/) that is a location
that is relative to the web site root or to the web application root (note
that ~ is processed server side i.e. it will work if processed by an ASP.NET
control or explicitely resolved).

That doesn't work. If I use backslashes, it starts from c:\. If I use
the a forward slashes, it starts in Windows\System32. ~ doesn't make
any difference.
 
B

brett

This is what I want:

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, myRelativePath);
 
P

Patrice

humm...Not sure I understood the initial question.

To me a "link" is an hyperlink. It looked like to me you were trying to
build (hyper)links in a web page referring to the server you are running on.

Now It looks like to me you want to find the physical location in the file
system that match a particular url location. Also I don't see how it has
something to do with the domain name...

Usually this is done with Server.MapPath that will take also in to account
virtual directories. For example if you create a virtual directory that maps
to a network share (or redirect to some local location), using
Server.MapPath will return the appropriate location (while the Path.Combine
method will return an improper location as it always assume that it is under
the web site root)

For example :
- create a site in c:\inetpub\wwwroot
- create an image directory below
- create a shared virtual dir "test" that maps to c:\tmp

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "\images"); and
Server.MapPath("/images") will return "c:\inetpub\wwwroot\images")
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "\test"); will return
"c:\inetpub\wwwroot\test" while Server.MapPath("/test") will return
"c:\temp")

Server.MapPath("~/test") will return the same path.

Now if you move this site in a "subweb" directory Server.MapPath("/images")
will still return "c:\inetpub\wwwroot\images" as this is related to the web
site root while server.MapPath("~/images") will return
"c:\inetpub\wwwroot\subweb\images" as this is related to the web application
root.
 
B

brett

Patrice said:
humm...Not sure I understood the initial question.

To me a "link" is an hyperlink. It looked like to me you were trying to
build (hyper)links in a web page referring to the server you are running on.

Now It looks like to me you want to find the physical location in the file
system that match a particular url location. Also I don't see how it has
something to do with the domain name...

Sorry. I wasn't so clear on it. It is a physical file I'm accessing
and displaying the contents of in a webform. The website folder
structure is always the same but its location changes depending on the
server. Do you forsee any issues with how I'm using it now? It seems
to work fine.

Thanks,
Brett
 
K

Kodali Ranganadh

Hi, Brett

Use
System.DiagnoticService.ActiveDirectory.Domain.GetCurrentDomain().GetDomainEnry().path
this is a static calss get the current system domain path, So this will
be give a flexeble if the file path has the same in both servers.

all the best .
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top