本文介绍了删除继承的ASP.NET Core应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的启动程序中具有以下设置:

I have the following setup in my Startup:

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", true, true);
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true)

appsettings.json:

appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
}

我知道我可以通过添加具有不同值的匹配JSON结构并省略我想要继承的值来覆盖给定环境的有效设置. appsettings.Development.json:

I know I can override the effective settings for a given environment by adding a matching JSON structure with different values and omitting those I want to inherit, e.g. appsettings.Development.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
}

但是我可以删除一个条目或一个节,除非用空值覆盖每个value属性?

But can I remove an entry or a section except by overriding each value property with an empty value?

-S

推荐答案

根据在ASP.NET Core文档中配置,设置是从两个文件中加载的,如果在两个文件中都指定了设置,则后者,即您设置中的appsettings.{env.EnvironmentName}.json,将覆盖第一个.

According to Configuration in ASP.NET Core documentation, settings are loaded from both files and if a setting is specified in both on them, than the one in the latter, i.e. appsettings.{env.EnvironmentName}.json in your setup, will override the first one.

因此,如果在appsettings.json文件中指定了一个设置,并且想要在 Development 环境中运行时将其删除,则需要相应地进行显式设置(",{}等等,具体取决于设置).

So, if a setting is specified in the appsettings.json file and you want to remove it when running in the Development environment, you will need to explicitly set that accordingly ("", {}, etc. depending on the setting) in the appsettings.Development.json file.

但是,这样的设置可能表明您的设置不应位于通用的appsettings.json文件中,而应直接位于特定环境的配置设置文件中.这样,编写设置可能会更容易.

However, such setup may suggest your setting should not lie in the generic appsettings.json file, but in the specific environments configuration settings file directly.This way, it might be easier to write your settings.

这篇关于删除继承的ASP.NET Core应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:15