如何确定 .net 核心 Web 应用程序的运行时

how to determine runtime for .net core web app

我正在尝试将 mvc 应用程序迁移到 .net 核心应用程序。在我的 mvc 应用程序中,我使用 Ninject 来设置我的数据源和连接字符串。逻辑是这样的:

public static string GetConnectString(IContext context){
      var httpContext = context.Kernel.Get<HttpContextBase>();

      if (ConfigurationManager.AppSettings["environmentMode"] == "local")
          return DataConnect.LocalConnectString;

      if (ConfigurationManager.AppSettings["environmentMode"] == "development")
          return DataConnect.StagingConnectString;

      return DataConnect.LiveConnectString;
}

这是在我的 NinjectWebCommon.cs class 中并从 RegisterServices 方法中调用的。

所以现在在 .net 核心中我有一个 Startup.cs 和 ConfigureServices 方法所以我可以做类似的事情:

services.AddDbContext<DBContext>(opt => opt.UseSqlServer(DataConnect.StagingConnectString));

但我不确定如何确定环境(本地、暂存或实时)以提供正确的连接字符串。

配置文件是appsettings.json,你可以有多个文件,比如appsettings.Production.json等等,将加载并覆盖 appsettings.json 中的设置。