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

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

问题描述

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

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.

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

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?

谢谢!

推荐答案


但是链接设置是什么,以便在发布应用程序时它们将覆盖
appsettings.json?

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

appsettings由 Program.cs

The appsettings are configured by WebHost in Program.cs

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

webhost.cs 该框架将这些设置添加到了webhost:

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

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

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.

发布时您的应用程序可以使用同名的OS环境变量覆盖此值。 .NET Core从中读取值并具有最后一个获胜策略。

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.

该层次结构当前为:



  1. 文件(appsettings.json,appsettings。{Environment} .json,其中{Environment}是应用程序的当前托管环境)

  2. Azure Key Vault

  3. 用户机密(秘密管理器)(仅在开发环境中)

  4. 环境变量
  5. 命令行参数

  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


因此,当您发布应用程序时您可以使用环境变量在主机OS上覆盖环境。当.NET Core启动您已发布的应用程序时,它将读取该变量并加载适当的appsettings。{environment} .json文件。如果未设置该值或该环境没有文件,则将应用 appsettings.json 中的设置。

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.

您可以阅读有关 ASPNETCORE_ENVIROMENT 设置此处

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

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