我有一个共同的网络项目,可以作为几个孩子的基础"网络项目。是否可以在项目之间应用web.config转换/合并?让我们说结构看起来像这样:
base project
- web.config
child project
- web.config
- transform.config
是否可以制作预构建事件或类似项,将基础项目web.config与子项目web.config合并?
答案 0 :(得分:5)
您可以编辑为单独的文件(transform.config) [1],[2] 和:
添加
<appsettings>
部分,并将您的数据添加为表单的键/值对:<add key="xxx" value="xxxx" />
这就是创建包含设置的新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>
连接字符串 [3] :
也是如此<connectionStrings>
<add name="yourConnectionStringName"
connectionString="yourConnectionString"
providerName="System.Data.SqlClient"/>
</connectionStrings>
并使用configSource
作为父文件:
<connectionStrings configSource="parentWeb.config"/>
答案 1 :(得分:1)
我最近遇到了这种问题 - 虽然我试图在运行时以编程方式合并两个web.config文件。
以下代码部分工作,对于AppSettings和Settings部分(必须添加ConnectionStrings),对于构建时间,您可以将其包装在可执行文件中:
protected void Application_BeginRequest(object sender, EventArgs e)
{
var p = Server.MapPath("~/Web.user.config");
if (File.Exists(p))
{
var fileMap = new ConfigurationFileMap(p); //Path to your config file
var userConfig = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
var globalConfig = WebConfigurationManager.OpenWebConfiguration("~/");
var globalGroups = globalConfig.SectionGroups.Cast<ConfigurationSectionGroup>().ToList();
CopySections(userConfig.Sections.Cast<ConfigurationSection>(), globalConfig.Sections.Cast<ConfigurationSection>());
foreach (ConfigurationSectionGroup userGroup in userConfig.SectionGroups)
{
var globalGroup = globalGroups.SingleOrDefault(g => g.SectionGroupName == userGroup.SectionGroupName);
if (globalGroup != null)
{
CopySections(userGroup.Sections.Cast<ConfigurationSection>(), globalGroup.Sections.Cast<ConfigurationSection>());
}
}
globalConfig.Save();
}
}
private void CopySections(IEnumerable<ConfigurationSection> source, IEnumerable<ConfigurationSection> target)
{
foreach (var sourceSection in source)
{
var targetSection = target.SingleOrDefault(s => s.SectionInformation.SectionName == sourceSection.SectionInformation.SectionName);
if (targetSection != null)
{
var targetAppSettings = targetSection as AppSettingsSection;
if (targetAppSettings != null)
{
var sourceAppSettings = (AppSettingsSection) sourceSection;
foreach (KeyValueConfigurationElement keyValue in sourceAppSettings.Settings)
{
var targetSettings = targetAppSettings.Settings;
if (targetSettings.AllKeys.Any(k => k == keyValue.Key))
{
targetSettings.Remove(keyValue.Key);
}
targetSettings.Add(keyValue);
}
}
var targetClientSettings = targetSection as ClientSettingsSection;
if (targetClientSettings != null)
{
var sourceClientSettings = (ClientSettingsSection) sourceSection;
foreach (SettingElement keyValue in sourceClientSettings.Settings)
{
var targetSettings = targetClientSettings.Settings;
var existingSetting = targetSettings.Cast<SettingElement>().SingleOrDefault(e => e.Name == keyValue.Name);
if (existingSetting != null)
{
existingSetting.Value = keyValue.Value;
}
}
}
}
}
}