在项目之间合并web.configs

fil*_*lur 13 c# asp.net web-config web.config-transform

我有一个常见的Web项目,用作几个"子"Web项目的基础.是否可以在项目之间应用web.config转换/合并?假设结构如下:

base project
  - web.config

child project
  - web.config
    - transform.config
Run Code Online (Sandbox Code Playgroud)

是否可以进行预构建事件或类似事件,将基础项目web.config与子项目web.config合并?

537*_*037 5

您可以编辑到单独的文件(transform.config)[1],[2]和:

添加一个<appsettings>部分并将您的数据添加为表单的键/值对:

<add key="xxx" value="xxxx" />
Run Code Online (Sandbox Code Playgroud)

这就是创建包含设置的新app.config文件的全部内容.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Child1" value="Child1_Value" />
    <add key="Child2" value="Child2_Value" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

对于连接字符串[3]也是如此:

<connectionStrings>
    <add name="yourConnectionStringName" 
         connectionString="yourConnectionString" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

configSource用于父文件:

<connectionStrings configSource="parentWeb.config"/>
Run Code Online (Sandbox Code Playgroud)