Last active
April 12, 2021 19:59
-
-
Save manoj-choudhari-git/e108a16b2c7a96bebde46bff6d8beaa2 to your computer and use it in GitHub Desktop.
Set ASPNETCORE_ENVIRONMENT variable from the application code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Hosting; | |
namespace EnvironmentSample | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
// Assuming args[0] has environment name | |
// Command to run: {envName} should be replaced by env name | |
// dotnet run {envName} | |
var environment = string.IsNullOrEmpty(args[0]) ? "Production" : args[0].Trim(); | |
CreateHostBuilder(args).UseEnvironment(environment).Build().Run(); | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment