.Net Core 通过选择配置在应用程序设置之间自动更改
.Net Core automatic change between appsettings by selecting configuration
我有很多连接字符串,我需要在 Live
、Report
和 Test
等不同场景中使用它们,只需更改 Visual Studio 中的解决方案配置即可。我花了很多时间,但我想不出解决办法。有什么想法吗?
提前致谢。
您可以针对不同的环境使用不同的应用程序设置,例如:
appsettings.Development.json
appsettings.Staging.json
appsettings.Production.json
- 等...
然后添加具有不同环境的不同配置文件以在 launchSettings.json
中启动您的应用程序
launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49364",
"sslPort": 0
}
},
"profiles": {
"IIS Express Development": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express Staging": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
},
"IIS Express Production": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
除了@Kahbazi 的回答,我还想像这样更新我的回答
在你的情况下你可以使用Options pattern in ASP.NET Core
例如你有 appsetting.json 具有这样的结构
"Development": {
"Url": "some string"
},
"Stage": {
"Url": "some string"
},
"Prod": {
"Url": "some string"
}
我需要像这样定义一个模型class来存储信息
public class Development
{
public string Url{ get; set; }
}
所以我们有了配置结构。我们需要做的最后一件事是在 Startup.cs
:
中注册
services.Configure<Development>(configuration.GetSection("Development"));
所以你可以这样使用:
private readonly IOptions<Development> _webAppUrl;
public EmailSender(IOptions<Development> _webAppUrl)
{
_webAppUrl = _webAppUrl;
}
_webAppUrl.Value.Url;
这就是 Microsoft 设置多个 appsetting.json
files 的方式。你可以看看因为它有点长post
以下是我如何根据环境使用 json 文件进行配置:
public Startup(
IConfiguration configuration,
IHostingEnvironment hostingEnvironment)
{
_configuration = configuration;
_hostingEnvironment = hostingEnvironment;
var builder = new ConfigurationBuilder();
if (_hostingEnvironment.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
else
{
builder
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
}
}
我有很多连接字符串,我需要在 Live
、Report
和 Test
等不同场景中使用它们,只需更改 Visual Studio 中的解决方案配置即可。我花了很多时间,但我想不出解决办法。有什么想法吗?
提前致谢。
您可以针对不同的环境使用不同的应用程序设置,例如:
appsettings.Development.json
appsettings.Staging.json
appsettings.Production.json
- 等...
然后添加具有不同环境的不同配置文件以在 launchSettings.json
launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49364",
"sslPort": 0
}
},
"profiles": {
"IIS Express Development": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express Staging": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
},
"IIS Express Production": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
除了@Kahbazi 的回答,我还想像这样更新我的回答
在你的情况下你可以使用Options pattern in ASP.NET Core
例如你有 appsetting.json 具有这样的结构
"Development": {
"Url": "some string"
},
"Stage": {
"Url": "some string"
},
"Prod": {
"Url": "some string"
}
我需要像这样定义一个模型class来存储信息
public class Development
{
public string Url{ get; set; }
}
所以我们有了配置结构。我们需要做的最后一件事是在 Startup.cs
:
services.Configure<Development>(configuration.GetSection("Development"));
所以你可以这样使用:
private readonly IOptions<Development> _webAppUrl;
public EmailSender(IOptions<Development> _webAppUrl)
{
_webAppUrl = _webAppUrl;
}
_webAppUrl.Value.Url;
这就是 Microsoft 设置多个 appsetting.json
files 的方式。你可以看看因为它有点长post
以下是我如何根据环境使用 json 文件进行配置:
public Startup(
IConfiguration configuration,
IHostingEnvironment hostingEnvironment)
{
_configuration = configuration;
_hostingEnvironment = hostingEnvironment;
var builder = new ConfigurationBuilder();
if (_hostingEnvironment.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
else
{
builder
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
}
}