Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging web.configs between projects

I have a common web project which is used as a base for several "child" web projects. Is it possible to apply a web.config transform/merge between projects? Let's say the structure looks like this:

base project
  - web.config

child project
  - web.config
    - transform.config

Is it possible to make a pre build event or similar that merges the base project web.config with the child project web.config?

like image 263
filur Avatar asked Jan 02 '18 13:01

filur


People also ask

Can I use multiple web config file in single project?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration.

How do I use different web config files?

config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release. If you want to transform other files you can do that too.


3 Answers

You can edit into separate file (transform.config) [1],[2] and:

Add an <appsettings> section and add your data as key/value pairs of the form:

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

That's all to create new app.config file with settings in it.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Child1" value="Child1_Value" />
    <add key="Child2" value="Child2_Value" />
  </appSettings>
</configuration>

And same for the connection string [3]:

<connectionStrings>
    <add name="yourConnectionStringName" 
         connectionString="yourConnectionString" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

And use configSource for the parent file:

<connectionStrings configSource="parentWeb.config"/>
like image 171
stefan Avatar answered Sep 22 '22 15:09

stefan


You can edit into separate file (transform.config) [1],[2] and:

Add an <appsettings> section and add your data as key/value pairs of the form:

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

That's all to create new app.config file with settings in it.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Child1" value="Child1_Value" />
    <add key="Child2" value="Child2_Value" />
  </appSettings>
</configuration>

And same for the connection string [3]:

<connectionStrings>
    <add name="yourConnectionStringName" 
         connectionString="yourConnectionString" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

And use configSource for the parent file:

<connectionStrings configSource="parentWeb.config"/>
like image 5
youpilat13 Avatar answered Oct 26 '22 10:10

youpilat13


I've struggled recently with this kind of problem - though I was trying to merge two web.config files programmatically at runtime.

The following code works partially, for AppSettings and Settings sections (ConnectionStrings would have to be added), for build time you could wrap it in an executable:

    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;
                        }
                    }
                }
            }
        }
    }
like image 1
Pawel Gorczynski Avatar answered Oct 26 '22 10:10

Pawel Gorczynski