英文:
How can I prevent appsettings.json values from being overridden when running on localhost in .NET 7 Web API?
问题
我帮你翻译以下部分:
"Localhost"配置值在本地主机运行时应保持不变。我已经为其他环境添加了单独的appsettings.{env}.json
文件。我只想在开发环境中使用appsettings.Development.json
文件,而不是在本地主机上使用。
我需要创建单独的appsettings.Localhost.json
文件吗?或者我可以像下面这样创建新的配置文件(Localhost)?请建议。
"Localhost": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7055;http://localhost:5065",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Localhost"
}
}
以下是我的 Program.cs 文件:
var builder = WebApplication.CreateBuilder(args);
{
// 环境配置
var configuration = builder.Configuration;
var env = builder.Environment;
configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Program>());
builder.Services.AddHttpClient();
builder.Services.AddApplication();
builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddHelpers();
builder.Services.AddControllers()
// 为控制器配置 JSON 序列化选项。
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
}
请注意,我已经将代码部分保持不变,只翻译了您提供的文本。
英文:
I want the configuration values from appsettings.json to remain unchanged when running on localhost. I have already added separate appsettings.{env}.json
files to other environments. I want to use appsettings.Development.json
file only for Dev environment not for localhost.
Do i need to create separate appsettings.Localhost.json
file? Or Can I create new profile (Localhost) like below? Please suggest.
"Localhost": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7055;http://localhost:5065",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Localhost"
}
}
Below is my Program.cs
var builder = WebApplication.CreateBuilder(args);
{
// Environment configuration
var configuration = builder.Configuration;
var env = builder.Environment;
configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Program>());
builder.Services.AddHttpClient();
builder.Services.AddApplication();
builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddHelpers();
builder.Services.AddControllers()
// Configures the JSON serialization options for controllers.
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
}
答案1
得分: 1
我在我的一侧有一个测试,我在 appsettings.json 中添加了一个配置。当我将启动设置更改为 Localhost
时,它会读取 appsettings.Localhost.json 中的变量。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论