Is there an alternate to Web.config Transormations in Asp.NET Core appsettings.json?
在Asp.NET Core appsettings.json中是否有替代Web.config Transormations?
I would like to change connection strings and other settins when deploying to staging.production server from VS.
我想从VS部署到staging.production服务器时更改连接字符串和其他设置。
2 个解决方案
#1
1
First of all, keep in mind, that
首先,请记住,那
ASP.NET Core’s configuration system has been re-architected from previous versions of ASP.NET, which relied on
System.Configuration
and XML configuration files like web.config. The new configuration model provides streamlined access to key/value based settings that can be retrieved from a variety of sources.ASP.NET Core的配置系统已经从以前版本的ASP.NET重新构建,它依赖于System.Configuration和XML配置文件,如web.config。新配置模型提供了对可从各种来源检索的基于键/值的设置的简化访问。
To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. At its simplest, Configuration is just a collection of sources, which provide the ability to read and write name/value pairs.
要在ASP.NET应用程序中使用设置,建议您仅在应用程序的Startup类中实例化Configuration。最简单的,Configuration只是一个源集合,它提供了读取和写入名称/值对的能力。
ASP.NET Core provides built-in support for JSON, XML, and INI configuration files as sources and allows to choose source accordinly to current environment. The ASPNETCORE_ENVIRONMENT
environment variable is used to setup/determine the current environment. Predefined values are Development
, Staging
, Production
, but can be any custom also.
ASP.NET Core提供对JSON,XML和INI配置文件的内置支持作为源,并允许根据当前环境选择源。 ASPNETCORE_ENVIRONMENT环境变量用于设置/确定当前环境。预定义值是开发,暂存,生产,但也可以是任何自定义。
Lets say you want to use JSON file (named appsettings.json
for convention) for storing connection string. What you need to do is to create a separate appsettings.{EnvironmentName}.json
files (for each environment you have) like:
假设您要使用JSON文件(名为appsettings.json for convention)来存储连接字符串。您需要做的是创建一个单独的appsettings。{EnvironmentName} .json文件(对于您拥有的每个环境),例如:
appsettings.Development.json
- appsettings.Development.json
appsettings.Staging.json
- appsettings.Staging.json
appsettings.Production.json
- appsettings.Production.json
and load one of them accordingly to environment:
并根据环境加载其中一个:
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Read more about all of this in the “Configuration” chapter and the “Working with multiple environments” chapter of the ASP.NET Core documentation.
在“配置”一章和ASP.NET核心文档的“使用多个环境”一章中阅读有关所有这些内容的更多信息。
#2
0
Yes, you must use environment variables on the target system.
是的,您必须在目标系统上使用环境变量。
ASP.NET Core references a particular environment variable, ASPNETCORE_ENVIRONMENT to describe the environment the application is currently running in. This variable can be set to any value you like, but three values are used by convention: Development, Staging, and Production. You will find these values used in the samples and templates provided with ASP.NET Core.
ASP.NET Core引用了一个特定的环境变量ASPNETCORE_ENVIRONMENT来描述应用程序当前运行的环境。该变量可以设置为您喜欢的任何值,但约定使用三个值:Development,Staging和Production。您将在ASP.NET Core提供的示例和模板中找到这些值。
Determining the environment at runtime
在运行时确定环境
The IHostingEnvironment service provides the core abstraction for working with environments. This service is provided by the ASP.NET hosting layer, and can be injected into your startup logic via Dependency Injection. The ASP.NET Core web site template in Visual Studio uses this approach to load environment-specific configuration files (if present) and to customize the app’s error handling settings. In both cases, this behavior is achieved by referring to the currently specified environment by calling EnvironmentName or IsEnvironment on the instance of IHostingEnvironment passed into the appropriate method.
IHostingEnvironment服务提供了用于处理环境的核心抽象。此服务由ASP.NET托管层提供,可以通过依赖注入注入到启动逻辑中。 Visual Studio中的ASP.NET Core网站模板使用此方法加载特定于环境的配置文件(如果存在)并自定义应用程序的错误处理设置。在这两种情况下,通过在传递给适当方法的IHostingEnvironment实例上调用EnvironmentName或IsEnvironment来引用当前指定的环境来实现此行为。
https://docs.asp.net/en/latest/fundamentals/environments.html
https://docs.asp.net/en/latest/fundamentals/environments.html
#1
1
First of all, keep in mind, that
首先,请记住,那
ASP.NET Core’s configuration system has been re-architected from previous versions of ASP.NET, which relied on
System.Configuration
and XML configuration files like web.config. The new configuration model provides streamlined access to key/value based settings that can be retrieved from a variety of sources.ASP.NET Core的配置系统已经从以前版本的ASP.NET重新构建,它依赖于System.Configuration和XML配置文件,如web.config。新配置模型提供了对可从各种来源检索的基于键/值的设置的简化访问。
To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. At its simplest, Configuration is just a collection of sources, which provide the ability to read and write name/value pairs.
要在ASP.NET应用程序中使用设置,建议您仅在应用程序的Startup类中实例化Configuration。最简单的,Configuration只是一个源集合,它提供了读取和写入名称/值对的能力。
ASP.NET Core provides built-in support for JSON, XML, and INI configuration files as sources and allows to choose source accordinly to current environment. The ASPNETCORE_ENVIRONMENT
environment variable is used to setup/determine the current environment. Predefined values are Development
, Staging
, Production
, but can be any custom also.
ASP.NET Core提供对JSON,XML和INI配置文件的内置支持作为源,并允许根据当前环境选择源。 ASPNETCORE_ENVIRONMENT环境变量用于设置/确定当前环境。预定义值是开发,暂存,生产,但也可以是任何自定义。
Lets say you want to use JSON file (named appsettings.json
for convention) for storing connection string. What you need to do is to create a separate appsettings.{EnvironmentName}.json
files (for each environment you have) like:
假设您要使用JSON文件(名为appsettings.json for convention)来存储连接字符串。您需要做的是创建一个单独的appsettings。{EnvironmentName} .json文件(对于您拥有的每个环境),例如:
appsettings.Development.json
- appsettings.Development.json
appsettings.Staging.json
- appsettings.Staging.json
appsettings.Production.json
- appsettings.Production.json
and load one of them accordingly to environment:
并根据环境加载其中一个:
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Read more about all of this in the “Configuration” chapter and the “Working with multiple environments” chapter of the ASP.NET Core documentation.
在“配置”一章和ASP.NET核心文档的“使用多个环境”一章中阅读有关所有这些内容的更多信息。
#2
0
Yes, you must use environment variables on the target system.
是的,您必须在目标系统上使用环境变量。
ASP.NET Core references a particular environment variable, ASPNETCORE_ENVIRONMENT to describe the environment the application is currently running in. This variable can be set to any value you like, but three values are used by convention: Development, Staging, and Production. You will find these values used in the samples and templates provided with ASP.NET Core.
ASP.NET Core引用了一个特定的环境变量ASPNETCORE_ENVIRONMENT来描述应用程序当前运行的环境。该变量可以设置为您喜欢的任何值,但约定使用三个值:Development,Staging和Production。您将在ASP.NET Core提供的示例和模板中找到这些值。
Determining the environment at runtime
在运行时确定环境
The IHostingEnvironment service provides the core abstraction for working with environments. This service is provided by the ASP.NET hosting layer, and can be injected into your startup logic via Dependency Injection. The ASP.NET Core web site template in Visual Studio uses this approach to load environment-specific configuration files (if present) and to customize the app’s error handling settings. In both cases, this behavior is achieved by referring to the currently specified environment by calling EnvironmentName or IsEnvironment on the instance of IHostingEnvironment passed into the appropriate method.
IHostingEnvironment服务提供了用于处理环境的核心抽象。此服务由ASP.NET托管层提供,可以通过依赖注入注入到启动逻辑中。 Visual Studio中的ASP.NET Core网站模板使用此方法加载特定于环境的配置文件(如果存在)并自定义应用程序的错误处理设置。在这两种情况下,通过在传递给适当方法的IHostingEnvironment实例上调用EnvironmentName或IsEnvironment来引用当前指定的环境来实现此行为。
https://docs.asp.net/en/latest/fundamentals/environments.html
https://docs.asp.net/en/latest/fundamentals/environments.html