Switching Domain name depending on server?

Discussion in 'ASP .Net' started by brett, Nov 30, 2006.

  1. brett

    brett Guest

    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
    brett, Nov 30, 2006
    #1
    1. Advertising

  2. 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/

    brett wrote:
    > 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
    Brennan Stehling, Nov 30, 2006
    #2
    1. Advertising

  3. brett

    Patrice Guest

    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...

    ---
    Patrice

    "brett" <> a écrit dans le message de news:
    ...
    >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
    >
    Patrice, Nov 30, 2006
    #3
  4. brett

    brett Guest

    Brennan Stehling wrote:
    > 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
    brett, Nov 30, 2006
    #4
  5. brett

    brett Guest

    Patrice wrote:
    > 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
    brett, Nov 30, 2006
    #5
  6. 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/

    brett wrote:
    > Patrice wrote:
    > > 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
    Brennan Stehling, Nov 30, 2006
    #6
  7. brett

    Patrice Guest

    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).

    --
    Patrice

    "brett" <> a écrit dans le message de news:
    ...
    >
    > Patrice wrote:
    >> 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
    >
    Patrice, Nov 30, 2006
    #7
  8. brett

    brett Guest

    Patrice wrote:
    > 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.
    brett, Dec 1, 2006
    #8
  9. brett

    brett Guest

    This is what I want:

    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, myRelativePath);
    brett, Dec 1, 2006
    #9
  10. brett

    Patrice Guest

    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.

    --
    Patrice

    "brett" <> a écrit dans le message de news:
    ...
    > This is what I want:
    >
    > Path.Combine(AppDomain.CurrentDomain.BaseDirectory, myRelativePath);
    >
    Patrice, Dec 1, 2006
    #10
  11. brett

    brett Guest

    Patrice wrote:
    > 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
    brett, Dec 1, 2006
    #11
  12. 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 .
    On Nov 30, 8:21 pm, "brett" <> wrote:
    > 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
    Kodali Ranganadh, Dec 2, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Infant Newbie
    Replies:
    2
    Views:
    3,238
    Infant Newbie
    Nov 12, 2003
  2. Joey
    Replies:
    0
    Views:
    337
  3. Patrick

    Getting windows user-name without domain name

    Patrick, Dec 7, 2007, in forum: ASP .Net Security
    Replies:
    2
    Views:
    520
    Nikolay Podkolzin
    Dec 14, 2007
  4. Jimmy
    Replies:
    0
    Views:
    104
    Jimmy
    Aug 31, 2004
  5. Chem Leakhina
    Replies:
    2
    Views:
    130
    Robert Klemme
    Jun 23, 2009
Loading...

Share This Page