How to refer to main domain without hard-coding its name?

后端 未结 4 2071
半阙折子戏
半阙折子戏 2021-02-19 22:01

I have a domain name: TestSite.com. I create several subdomains for this site and refer to them as first.TestSite.com, second.TestSite.com, etc.

How do I refer to TestSi

4条回答
  •  臣服心动
    2021-02-19 22:20

    Since you tagged your question ASP.NET, I'm going to assume you want to do this server-side, so you are not relying on JavaScript on the client-side to generate HTML links.

    First of all, make sure you have a web.config file in your subdomain sites, and add this to the configuration:

    
      
         
      
    
    

    Using this setting in your ASPX pages, hyperlinks on one of your subdomains' pages would look like this:

    Go to main domain home page
    Go to products page on the main domain
    

    The setting with name="MainDomain" will be read from the configuration file, and inserted into the page where it says <%$ AppSettings: MainDomain %>. This way, you are creating the required HTML on the server-side, with no hard-coding required.

    If you need to change the name, simply update the configuration file, and all pages will automatically use the new domain name.

提交回复
热议问题