Asp.net manages multiple web.config files

Can anyone suggest a good way to manage multiple web.config files?

For example, I have a web.config development that I use in my local machine called localWeb.config and then I use for production called "prodWeb.config". I keep the .config extensions so no one can access them from the web server.

localWeb.config will, for example, have a connection string to the development database, and prodWeb.config will have a connection to the production database.

I am currently using the postbuild event, which will copy the contents of localWeb.config or prodWeb.config to web.config. This works well, but I was wondering if anyone knew about the best ways.

+3
source share
4 answers

Check out the Web Deployment Tool :

Here are just a few of the tasks that can be performed with the tool:

  • Create a package that contains content, configuration, and SQL databases for deployment or sharing with others.

  • Use the package as a way to version your application or create backups.

  • Add options to replace the connection string or other value in the file during package installation.

  • Allow non-administrators to deploy packages and control their access in detail.

  • Synchronization or migration on both sites and servers running IIS 6.0 and IIS 7.0.

+2
source

, , . - "prodsrvr- *", , db .. "test- *", . ..

EDIT: , , .

public static string GetConnString()
{
    string connString = ConfigurationSettings.AppSettings[GetConfigKey("database")];
    return connString;
}

public static string GetConfigKey(string baseKey)
{
    string str = baseKey;
    if (Dns.GetHostName().StartsWith("dinoch"))
    {
        str = str + "-dev";
    }
    else if (Dns.GetHostName().StartsWith("prodsrvr"))
    {
        str = str + "-prod";
    }
    return str;
}

<configuration>
   <appSettings>
     <add key="database-dev" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
     <add key="database-prod" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
   </appSettings>
</configuration>

StartsWith(), " " .

, web.config. , regex1 , 1. regex2, 2. .. , foreach(), , .

, , , , prod dev , . .

+1

this will continue until asp.net 4 arrives http://www.asp.net/learn/whitepapers/aspnet40/#_Toc223325501

+1
source

Source: https://habr.com/ru/post/1705519/


All Articles