如何使用 .NET Core 根据发布配置文件更新 appsettings.json? [英] How to update appsettings.json based on publish profile using .NET Core?

查看:50
本文介绍了如何使用 .NET Core 根据发布配置文件更新 appsettings.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从 .NET Framework 切换到 .NET Core.过去,我的所有应用程序设置都在 Web.config 文件中.当我添加一个新的发布配置文件时,我可以右键单击并选择添加配置转换",这将在 Web.config 下生成一个嵌套的 Web.{Profile}.config 文件,我可以在其中设置特定于相应配置文件的应用程序设置.

现在,在 .NET Core 中,我想使用 appsettings.json 文件而不是 Web.config 文件来实现相同的效果.如何创建 appsettings.{Profile}.json 文件,该文件将嵌套在我的 appsettings.json 文件下并包含特定于我的发布配置文件的设置?当然,我可以手动创建文件,但是是什么链接"了这些设置,以便它们在应用程序发布时覆盖 appsettings.json?在 Visual Studio 中是否有一种简单的方法可以像我在旧的 .NET Framework 项目中描述的那样?还是我遗漏了什么?

谢谢!

解决方案

但是是什么链接"了设置以便它们覆盖appsettings.json 应用何时发布?

应用设置由Program.cs

中的WebHost配置

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>WebHost.CreateDefaultBuilder(args).UseStartup();

在实现webhost/a> 框架将 appsettings 添加到 webhost:

config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

第二行是添加环境特定的appsettings.在 Visual Studio 中,此环境变量在 Project Settings -> 中定义.调试 页面,称为ASPNETCORE_ENVIRONMENT.默认情况下,它设置为 Development,因此当您在 Visual Studio 中构建和运行时,将加载 appsettings.development.json 文件(如果存在)并覆盖任何匹配的设置在 appsettings.json 文件中.

发布应用程序时,您可以使用同名的操作系统环境变量覆盖此值..NET Core 从中读取值的位置有一个层次结构,并具有最后一个获胜"策略.

当前的层次结构是:

<块引用>

  1. 文件(appsettings.json、appsettings.{Environment}.json,其中 {Environment} 是应用的当前托管环境)
  2. Azure Key Vault
  3. 用户机密(Secret Manager)(仅限开发环境)
  4. 环境变量
  5. 命令行参数

因此,当您发布应用程序时,您可以使用环境变量覆盖主机操作系统上的环境.当 .NET Core 启动您发布的应用程序时,它将读取该变量并加载相应的 appsettings.{environment}.json 文件.如果未设置该值,或者该环境不存在文件,则将应用 appsettings.json 中的设置.

您可以阅读有关 ASPNETCORE_ENVIROMENT 设置的更多信息 这里

I'm currently making the switch from .NET Framework to .NET Core. In the past, all of my application settings were in the Web.config file. When I added a new publish profile, I could right click and select 'Add Config Transform' which would generate a nested Web.{Profile}.config file under Web.config where I could set application settings that were specific to the respective profile.

Now, in .NET Core, I want to achieve the same effect using an appsettings.json file instead of Web.config file. How can I create an appsettings.{Profile}.json file which will be nested under my appsettings.json file and contain settings that are specific to my publish profile? Of course, I can create the file manually, but what is it that 'links' the settings so that they will override appsettings.json when the application is published? Is there a simple way to do this in Visual Studio like I described for my old .NET Framework projects? Or am I missing something?

Thanks!

解决方案

but what is it that 'links' the settings so that they will override appsettings.json when the application is published?

The appsettings are configured by WebHost in Program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>();

In the implementation of webhost.cs the framework adds the appsettings to the webhost:

config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

The second line is where it adds the environment specific appsettings. Within Visual Studio this environment variable is defined in the Project Settings -> Debug page and is called ASPNETCORE_ENVIRONMENT. By default it is set to Development so when you build and run within Visual Studio the appsettings.development.json file (if it exists) will be loaded and override any matching settings in the appsettings.json file.

When you publish your application you can override this value using an OS environment variable of the same name. There is an hierarchy of where .NET Core reads the values from and has a "last one wins " policy.

The hierarchy is currently:

  1. Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)
  2. Azure Key Vault
  3. User secrets (Secret Manager) (in the Development environment only)
  4. Environment variables
  5. Command-line arguments

So when you publish your app you can override the environment on the host OS using an environment variable. When .NET Core starts your published app it will read that variables and load the appropriate appsettings.{environment}.json file. IF the value is not set, or no file exists for that environment, then the settings in appsettings.json will apply.

You can read more about the ASPNETCORE_ENVIROMENT setting here

这篇关于如何使用 .NET Core 根据发布配置文件更新 appsettings.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆