How to set aspnetcore_environment in publish file?

后端 未结 6 794
盖世英雄少女心
盖世英雄少女心 2020-12-28 12:13

I have ASP.NET Core application (Web Api). The documentation has explained working with multiple environments, however it failed to explain how to set aspnetcore_envir

6条回答
  •  心在旅途
    2020-12-28 12:50

    To setup two or more profiles, you need to create additional profile, as mentioned in a linked article, and your launchSettings.json will contain an array:

    "profiles": {
      "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      },
      "IIS Express (Staging)": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Staging"
        }
      }
    }
    

    To be able to read the environment variable, you need to specify it during startup and call additional method AddEnvironmentVariables to variables take action:

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                // general properties
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                // specify the environment-based properties
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                // do not forget to add environment variables to your config!
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
    }
    

提交回复
热议问题