首页 > 解决方案 > 覆盖nuget包中的设置

问题描述

我有一个项目,我在其中创建了一个设置文件(右键单击属性并添加设置文件 - 见下图)。

在此处输入图像描述

然后我将此项目转换为我已安装到我的网站项目中的 nuget 包

通常在网站中,我可以将以下内容添加到我的 web.config 以覆盖项目的设置:

<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>

但是,对于 nuget,它只是使用发布 nuget 时编译的初始设置。有没有办法覆盖我的网络项目中的 nuget 设置?

标签: c#asp.net-mvcnugetapplication-settings

解决方案


不确定为什么设置覆盖不起作用,因为它们与其他 nugets 一起使用。

最后,我们将设置移动到 app.config 文件中,并编写了我们自己的单例以使用配置管理器读取设置:

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

这样我们可以使用应用设置键在 web.config 中覆盖:

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

推荐阅读