简体   繁体   English

以编程方式更新各个站点的Web.config文件

[英]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. 我正在准备一个工具(Web应用程序),使用它我应该能够更改托管在不同Web服务器上的各种Web应用程序的web.config文件。

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. 我尝试使用WebConfigurationManager.OpenWebConfiguration(strings),但是它仅采用相对路径,但是如果我放置不同Web服务器web.config文件的共享位置,则它将失败。

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

I think, in my case i will not be able to use WebConfigurationManager.OpenWebConfiguration. 我认为,就我而言,我将无法使用WebConfigurationManager.OpenWebConfiguration。

Only option is XDocument. 唯一的选择是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 . 在过去的项目中,我成功使用了基本文件和转换文件: https : //msdn.microsoft.com/zh-cn/library/vstudio/dd465318(v=vs.100).aspx

As an example, we have a connection string in our web.config.base file like: 例如,在我们的web.config.base文件中有一个连接字符串,例如:

...
<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: 和web.config.transform文件中的条目,如下所示:

<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. 在构建应用程序时,将重新创建web.config文件,用本地转换文件中定义的任何连接字符串替换默认字符串。 The web.config.base file is committed to our versioning system, while the transform is not. web.config.base文件已提交到我们的版本控制系统,而转换未提交。

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. 因此,开发人员可以有一个转换文件连接到其本地数据库,qa服务器具有不同的转换文件,而演示服务器又具有不同的设置,所有这些操作的麻烦程度最小,因为大多数设置都保存在web.config.base中。文件,该文件随存储库一起传递,并且只需要维护从一个环境到另一个环境的连接字符串更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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