Search code examples
c#asp.net-mvcnugetapplication-settings

Override settings in nuget package


I have a project in which I have created a settings file (right click on properties and add settings file - see below image).

enter image description here

I have then converted this project into a nuget package which I have installed into my website project

Usually in the website, I can add the following to my web.config to override the settings of the project:

<configSections>
  <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <section name="Nuget.Navigation" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </sectionGroup>
</configSections>
<applicationSettings>
  <Nuget.Navigation>
    <setting name="SelectedSection" serializeAs="String">
      <value>Investors</value>
    </setting>
  </Nuget.Navigation>
</applicationSettings>

However, with the nuget, it is just using the initial setting that was compiled when the nuget was published. Is there a way to override the nuget setting in my web project?


Solution

  • Not sure why the settings overrides weren't working as they have worked with other nugets.

    In the end we moved the settings into an app.config file and programmed our own singleton to read the settings using Configuration Manager:

    SelectedSection = ConfigurationManager.AppSettings[nameof(SelectedSection)];
    

    This way we could override in the web.config using app settings keys:

    <add key="SelectedSection" value="investors" />