大火。 ASP.NET托管AppSettings.json

发布于 01-20 18:53 字数 1276 浏览 4 评论 0原文

在 Visual Studio 中从项目的服务器端运行我的项目时,我无法访问 appsettings.{env}.json。特定 env json 文件中的所有键都会在 builder.Configuration 对象上返回 null。它们仅加载到主 appsettings.json 文件中。对于特定的 json 文件,project.server 和 project.client 中的调试环境是相同的(非常确定客户端环境不重要)。没有看到其他人遇到这个问题,我错过了什么? 谢谢

[编辑]
.NET 6.0
以下是在 Project.Server 和 Project.Client 调试启动配置文件中设置的
ASPNETCORE_ENVIRONMENT=Project.Server 中的 Development

Program.cs Project.Server 中的

IConfiguration config = builder.Configuration;
var serverVersion = new MySqlServerVersion(new Version(10, 7));
builder.Services.AddDbContext<ProjectMariaDbContext>(
dbContextOptions => dbContextOptions
                    .UseMySql(config["ConnectionStrings:MariaDb"], serverVersion) );

appsettings.Development.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },
    "ConnectionStrings": {
      "MariaDb": "server=maria; port=3306; database=project; user=root; password=testing;"
    }
  }
}

在添加 dbcontext 行的断点处立即立即窗口中进行测试

config["ConnectionStrings:MariaDb"]
null

另请注意,我正在从 VS 中的 Project.Server 配置运行该项目

When running my project from the server side of the project in visual studio I am unable to access appsettings.{env}.json. All keys in the specific env json file return null on the builder.Configuration object. They only load in the main appsettings.json file. The environment for debugging is the same in the project.server and project.client (Pretty sure the client env shouldn't matter) for the particular json file. Haven't seen anybody else with this problem, what am I missing?
Thanks

[EDIT]
.NET 6.0
Below is set in both Project.Server and Project.Client Debug launch profiles
ASPNETCORE_ENVIRONMENT=Development

Program.cs in Project.Server

IConfiguration config = builder.Configuration;
var serverVersion = new MySqlServerVersion(new Version(10, 7));
builder.Services.AddDbContext<ProjectMariaDbContext>(
dbContextOptions => dbContextOptions
                    .UseMySql(config["ConnectionStrings:MariaDb"], serverVersion) );

appsettings.Development.json in Project.Server

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },
    "ConnectionStrings": {
      "MariaDb": "server=maria; port=3306; database=project; user=root; password=testing;"
    }
  }
}

Testing in the immediate Immediate window at a break point on adding dbcontext line

config["ConnectionStrings:MariaDb"]
null

Also to note I am running the project from the Project.Server configuration in VS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文