>我遇到的情况是我的 web.config 在配置文件部分中有一些属性
<profile>
...
<properties >
<clear/>
<add type="System.String" name="propData"/>
...
</properties>
</profile>
现在我想添加一些其他属性,但来自外部文件(保留当前 web.config 用于服务器设置,但每次构建部署外部配置文件)。有没有办法将外部文件中的属性合并到我的 web.config 中,以便我得到
<profile>
...
<properties >
<clear/>
<add type="System.String" name="propData"/>
...
<add type="System.String" name="externalProp"/>
...
</properties>
</profile>
我可以使用一些web.config语法(如"configSource"或"file")来执行此操作吗?
您可以使用外部配置所需的其他配置更新 web.config,
Example
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
以编程方式操作 web.config