什么是ASP.NET Core中的Web.config转换的等效项?

编程入门 行业动态 更新时间:2024-10-26 08:29:44
本文介绍了什么是ASP.NET Core中的Web.config转换的等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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转换的等效项?

本文发布于:2023-11-13 23:41:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   Core   config   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!