C# 在项目之间合并web.configs

C# 在项目之间合并web.configs,c#,asp.net,web-config,web.config-transform,C#,Asp.net,Web Config,Web.config Transform,我有一个公共web项目,它被用作几个“子”web项目的基础。是否可以在项目之间应用web.config转换/合并?假设结构如下所示: base project - web.config child project - web.config - transform.config 是否可以创建一个预构建事件或类似事件,将基本project web.config与子project web.config合并?您可以编辑到单独的文件(transform.config)中,并且: 添加部

我有一个公共web项目,它被用作几个“子”web项目的基础。是否可以在项目之间应用web.config转换/合并?假设结构如下所示:

base project
  - web.config

child project
  - web.config
    - transform.config

是否可以创建一个预构建事件或类似事件,将基本project web.config与子project web.config合并?

您可以编辑到单独的文件(transform.config)中,并且:

添加
部分,并将数据添加为表单的键/值对:

<add key="xxx" value="xxxx" />

我最近一直在努力解决这类问题——尽管我试图在运行时以编程方式合并两个web.config文件

以下代码部分工作,对于AppSettings和Settings部分(必须添加ConnectionString),对于构建时,您可以将其包装到可执行文件中:

    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;
                        }
                    }
                }
            }
        }
    }
受保护的无效应用程序\u BeginRequest(对象发送方,事件参数e)
{
var p=Server.MapPath(“~/Web.user.config”);
如果(File.Exists(p))
{
var fileMap=new ConfigurationFileMap(p);//配置文件的路径
var userConfig=ConfigurationManager.OpenMappedMachineConfiguration(文件映射);
var globalConfig=WebConfigurationManager.OpenWebConfiguration(“~/”);
var globalGroups=globalConfig.SectionGroups.Cast().ToList();
CopySections(userConfig.Sections.Cast()、globalConfig.Sections.Cast());
foreach(userConfig.SectionGroups中的ConfigurationSectionGroup用户组)
{
var globalGroup=globalGroups.SingleOrDefault(g=>g.SectionGroupName==userGroup.SectionGroupName);
如果(全局组!=null)
{
CopySections(userGroup.Sections.Cast()、globalGroup.Sections.Cast());
}
}
globalConfig.Save();
}
}
私有void复制节(IEnumerable源、IEnumerable目标)
{
foreach(源中的var sourceSection)
{
var targetSection=target.SingleOrDefault(s=>s.SectionInformation.SectionName==sourceSection.SectionInformation.SectionName);
if(targetSection!=null)
{
var targetAppSettings=targetSection作为应用程序设置部分;
if(targetAppSettings!=null)
{
var sourceAppSettings=(AppSettingsSection)sourceSection;
foreach(sourceAppSettings.Settings中的KeyValueConfigurationElement keyValue)
{
var targetSettings=targetAppSettings.Settings;
if(targetSettings.AllKeys.Any(k=>k==keyValue.Key))
{
targetSettings.Remove(keyValue.Key);
}
targetSettings.Add(键值);
}
}
var targetClientSettings=targetSection作为ClientSettingsSection;
if(targetClientSettings!=null)
{
var sourceClientSettings=(ClientSettingsSection)sourceSection;
foreach(sourceClientSettings.Settings中的SettingElement键值)
{
var targetSettings=targetClientSettings.Settings;
var existingSetting=targetSettings.Cast().SingleOrDefault(e=>e.Name==keyValue.Name);
如果(现有设置!=null)
{
existingSetting.Value=keyValue.Value;
}
}
}
}
}
}

为什么不使用Nuget软件包将“核心”基础项目作为软件包填充,并覆盖客户所需的内容呢。通过这种方式,您可以在使用这些包的所有自定义应用程序的基础上填充更新。我从未使用过它,所以不确定它应该如何工作,但web.config自动从父文件夹中继承。no?因此,从理论上讲,您需要做的就是将这些项目放在父/子项目中folders@Marco谢谢,但我对涉及nuget的解决方案不感兴趣,因为我们没有nuget提要atm@ZivWeissman是的,它适用于某些设置,但不适用于连接stringsHow之类的东西。你会将子web配置与基本web配置合并吗?您的意思是子web配置是基本web配置的转换吗?i、 它是否具有转换属性,或者是常规web配置?如果它只是一个常规配置,那么在将子web配置合并到基本web配置之后,结果应该是什么?如果它本身是一个变换,那么至少很清楚如何合并。
<connectionStrings>
    <add name="yourConnectionStringName" 
         connectionString="yourConnectionString" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>
<connectionStrings configSource="parentWeb.config"/>
    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;
                        }
                    }
                }
            }
        }
    }