简体   繁体   English

有没有一种方法可以将web.config配置文件属性与外部配置文件合并?

[英]Is there a way to merge web.config profile properties with an external config file?

I have a situation where my web.config has some properties in the profile section 我的情况是我的web.config在配置文件部分中具有一些属性

<profile>
...
  <properties  >
        <clear/>
        <add   type="System.String" name="propData"/>
        ...
  </properties>
</profile>

Now I want to add some other properties but from an external file (preserving current web.config for server settings, but deploying the external config file per build). 现在,我想从外部文件中添加其他一些属性(保留当前的web.config用于服务器设置,但在每次构建时都部署外部配置文件)。 Is there a way to merge the properties from the external file into my web.config so that I get 有没有一种方法可以将属性从外部文件合并到我的web.config中,这样我就可以

<profile>
...
  <properties  >
        <clear/>
        <add   type="System.String" name="propData"/>
        ...
        <add type="System.String" name="externalProp"/>
        ... 
  </properties>
</profile>

Can I do this using some web.config syntax like "configSource" or "file"? 我可以使用某些web.config语法(例如“ configSource”或“ file”)来执行此操作吗?

You can update the web.config with required additional configurations from external config, 您可以使用外部配置中所需的其他配置更新web.config,

Example

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();

Programmatically manipulating web.config 以编程方式操纵web.config

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

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