ASP.NET Core文档建议我们应该使用appsettings.json文件以及每个环境的文件,其中应包含替代值.问题是所有这些文件都已发布,尽管只有appsettings.json和appsettings.[Environment] .json是相关的. 另一个问题是,要更改服务器上的配置值,必须检查两个文件:基本文件和特定于环境的文件.
ASP.NET Core documentation suggests we should use appsettings.json file, along with a file per environment, containing overriding values. The problem is that all these files are published, though only appsettings.json and appsettings.[Environment].json are relevant. The other problem is that to change a config value on server one must inspect both files: base and environment specific.
所以我的问题是:在每个部署环境中拥有一个配置文件的最干净的方法是什么?
So my question is: what is the cleanest way to have one configuration file in each deployment environment?
推荐答案主要区别在于,没有像ASP.NET应用程序那样为特定配置部署ASP.NET Core应用程序.以前,例如,您实际上是将其发布为发布",并且如果要切换部署配置,则必须重新发布.对于ASP.NET Core应用程序,环境由ASPNETCORE_ENVIRONMENT环境变量或在执行过程中传递的环境确定.因此,部署所有可能的环境设置是完全有效的,因为可以随时更改环境.
The key difference is that ASP.NET Core apps are not deployed for a particular configuration, as ASP.NET apps were. Previously, you'd literally publish as "Release", for example, and if you wanted to switch the deployment config, you'd have to republish. With an ASP.NET Core app, the environment is determined by the ASPNETCORE_ENVIRONMENT environment variable or by the environment passed during execution. As a result, it's perfectly valid to deploy all possible environmental settings, since the environment can be changed on a whim.
也就是说,如果您确定自己永远不会更改环境,则只需删除不需要的任何appsettings.{environment}.json文件.此外,您不应真正覆盖man appsettings.json中的设置.如果是特定于环境的,则应放入特定于环境的JSON文件之一.您的主appsettings.json文件实际上应该只包含不受环境影响的全局设置.如果以这种方式进行处理,则不必同时查看两者来查看设置的来源.
That said, if you know for sure you're never going to change the environment, you can simply remove any appsettings.{environment}.json files you don't need. Additionally, you should not really override settings in the man appsettings.json. If it's environment-specific, it should go in one of the environment-specific JSON files. Your main appsettings.json file should really only contain global settings unaffected by environment. If you handle it in this way, then you don't have to look at both to see where the settings are coming from.
最后,如果您通常不喜欢这种方法,则可以简单地选择其他配置策略,例如环境变量或外部配置源(例如Azure Key Vault).
Finally, if you don't like this approach, in general, you can simply choose a different configuration strategy, such as environment variables or an external config source such as Azure Key Vault.
更多推荐
什么是ASP.NET Core中的Web.config转换的等效项?
发布评论