# ASP .NET CORE 根据环境变量配置多个 appsettings.json
1、创建配置文件
全部文件属性设置为--复制到输出目录:始终复制。其中 Staging 和 Pre 是预生产环境,使用其中一个就行。
appsettings.Development.json
appsettings.Production.json
appsettings.Staging.json
appsettings.Pre.json
2、在 launchSettings.json
中添加如下启动项
"Production": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"Staging": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
},
"Development": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Pre": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Pre"
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
3、创建多环境读取配置文件的帮助类 MyConfigurationHelper
、AppsettingsHelper
/// <summary>
/// 全局配置管理类(适配多环境)
/// </summary>
public static class MyConfigurationHelper
{
public static IConfiguration Config { get; set; }
static MyConfigurationHelper()
{
var env = MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>();
Config = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true)
.Build();
}
}
public static class MyServiceProvider
{
public static IServiceProvider ServiceProvider { get; set; }
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private static IConfiguration _configuration;
static AppsettingsHelper()
{
//var env = MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>();
//Config = new ConfigurationBuilder()
// .SetBasePath(env.ContentRootPath)
// .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true)
// .Build();
var env = MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>();
//在当前目录或者根目录中寻找appsettings.json文件
var fileName = $"appsettings.{env.EnvironmentName}.json";
var directory = AppContext.BaseDirectory;
directory = directory.Replace("\\", "/");
var filePath = $"{directory}/{fileName}";
if (!File.Exists(filePath))
{
var length = directory.IndexOf("/bin");
filePath = $"{directory.Substring(0, length)}/{fileName}";
}
var builder = new ConfigurationBuilder()
.AddJsonFile(filePath, false, true);
_configuration = builder.Build();
}
public static string GetSectionValue(string key)
{
return _configuration.GetSection(key).Value;
}
public static string GetSectionValue(string key, string chidKey)
{
return _configuration.GetSection(key).GetSection(chidKey).Value;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
4、在 Startup.cs 中使用环境配置
//配置多环境
MyServiceProvider.ServiceProvider = app.ApplicationServices;
2
5、发布后设置环境变量——创建web.config
,为程序发布配置环境变量
无论 ASPNETCORE_ENVIRONMENT 设置为 Development、Staging、Production,只要项目中有 appsettings.Production.json,那项目发布后运行时默认会读取 Production 的配置。
无论 ASPNETCORE_ENVIRONMENT 设置为 Development、Staging、Production,只要项目中没有 appsettings.Production.json 那项目发布后运行时就会仅读取 appsettings.json 的配置。
若想在发布后动态设置环境变量,可以通过修改 web.config 的方式来实现,Core 项目中默认是没有 web.config 文件的,但是发布后会生成一个 web.config 文件,我们可以通过添加environmentVariable
节点来设置环境变量:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Skip.User.Api.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
<!--Development-->
<!--Staging-->
<!--Production-->
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
6、配置系统环境变量
# Windows
命令行:
setx ASPNETCORE_ENVIRONMENT "Development"
或者(需要管理员权限)
setx ASPNETCORE_ENVIRONMENT "Development" /M
PowerShell 命令:
Env:ASPNETCORE_ENVIRONMENT = "Prodction"
Windows 设置环境命令后,需要重新再开一个命令行 dotnet *.dll 启动项目,才会有效。
# Linux
使用 export 命令直接进行环境变量设置。
export ASPNETCORE_ENVIRONMEN='Production'
# Docker
Docker 配置最为简单,直接在启动容器的时候加上 -e 参数即可,例如:
docker run -d -e ASPNETCORE_ENVIRONMENT=Production --name testContainer testImage