You don't have to use environment variables if you adjust how the WebHostBuilder
processes its configuration. This is merely the default for dotnet new -t web
. For example, if you wanted to be able to set the default environment to "development" instead of production and facilitate overriding the environment in the command line, you could do that by modifying the normal Program.cs
code from this ...
public static void Main(string[] args) {
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://0.0.0.0:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
... into something like this ...
private static readonly Dictionary<string, string> defaults =
new Dictionary<string, string> {
{ WebHostDefaults.EnvironmentKey, "development" }
};
public static void Main(string[] args) {
var configuration =
new ConfigurationBuilder()
.AddInMemoryCollection(defaults)
.AddEnvironmentVariables("ASPNETCORE_")
.AddCommandLine(args)
.Build();
var host =
new WebHostBuilder()
.UseConfiguration(configuration)
.UseKestrel()
.UseUrls("http://0.0.0.0:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
Doing this, the environment variables would still work, but you can override it on the command line without any third-party dependencies, like so:
dotnet run environment=development
dotnet run environment=staging
This is actually what the yeoman generators do.