How do you deal with connection strings when deploying an ASP.NET site?

后端 未结 11 1034
暗喜
暗喜 2021-02-04 13:51

Right now our test and production databases are on the same server, but with different names. Deploying has meant editing Web.config to change all the connection strings for th

相关标签:
11条回答
  • 2021-02-04 14:22

    I usually have three separate web configs: one for my development machine, one for QA, and one for production. The development one connects to my local SQL database (which is firewalled from outside) and it is the default web.config. The others are named web-prod.config and web-qa.config. After publishing I delete the two that I don't need and rename the correct one to web.config. If I forget, the app breaks the first time it attempts to access the database, since the default config references one it can't get to.

    Since IIS refuses to serve up a file named .config, I make sure they all end in .config instead of say web.config-prod or web.config-qa.

    0 讨论(0)
  • 2021-02-04 14:25

    Here's another thing you can try:

    Using SQL Server Configuration Manager, make a db Alias for your development database so that the web.config file can be the same on both your development box and the production server.

    0 讨论(0)
  • 2021-02-04 14:26

    I did this so often, I made the web.config on the production server read-only.

    0 讨论(0)
  • 2021-02-04 14:36

    I've been in a few places now that store them in the registry.

    There's probably more elaborate ways to do it now but a lot of code I've worked on with a 1.0/1.1 heritage store the strings in the registry.

    The registry has a few advantages

    1. It keeps people from deploying the code to the wrong places since machines not configured properly will lack the keys
    2. It eliminates the problem wherein a developer will accidentally package a web.config file with the development connection strings in it (followed by a frantic phone call in the middle of the night wherein it is revealed that the late night sysadmin did not back up the previous web.config and the developer does not know or recall the production strings)
    3. It limits the possibility of a hacker being able to get the connection string by fetching the web.config off of the machine. Plus the registry has more levels of security than the filesystem.
    0 讨论(0)
  • 2021-02-04 14:40

    have environment folders with separate configs for each environment

    deploy out the correct one for the environment

    0 讨论(0)
  • 2021-02-04 14:41

    I create a database alias on each server to point to the database. I then use this alias in my web.config files. If I need to change which database the application points to, then I change the alias and not the web.config.

    For SQL Server, go to SQL Server Configuration Manager > SQL Native Client Configuration > Aliases > Create New Alias.

    You can do the same thing with Oracle with the tnsnames file.

    0 讨论(0)
提交回复
热议问题