简体   繁体   中英

Updating Web.config file of various sites programatically

I am preparing a single tool(web application) using which i should be able to change web.config file of various web applications hosted on different web servers.

I tried using WebConfigurationManager.OpenWebConfiguration(strings), but it takes only relative path but if i put shared location of different web servers web.config files, it fails.

MSDN ref: https://msdn.microsoft.com/en-us/library/ms151456(v=vs.110).aspx

I think, in my case i will not be able to use WebConfigurationManager.OpenWebConfiguration.

Only option is XDocument.

Please suggest.

In past projects, I have used base and transform files with success: https://msdn.microsoft.com/en-us/library/vstudio/dd465318(v=vs.100).aspx .

As an example, we have a connection string in our web.config.base file like:

...
<connectionStrings>
    <!-- Connection String Changes will be lost when this file is
         regenerated - Please edit your transform file instead -->
    <add 
        name="MainConnectionString" 
        connectionString="(default connection string)" />

and an entry in a web.config.transform file that looks like:

<connectionStrings>
<add name="MainConnectionString"
            connectionString="(system-specific connection string)"
            xdt:Transform="Replace"
            xdt:Locator="Match(name)"
                          />

The the web.config file is recreated when the application builds, with whatever connection string is defined in the local transform file replacing the default string. The web.config.base file is committed to our versioning system, while the transform is not.

So devs can have one transform file to connect to their local db's, qa servers have a different transform file, and demo servers have a different set again, all with a minimum of fuss, because most settings are kept in the web.config.base file, which is passed around with the repository, and only the connection string changes have to be maintained from one environment to the next.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM