您可以ASP.Net核心環境變量做到這一點(ASPNETCORE_ENVIRONMENT): -
using Microsoft.AspNetCore.Hosting;
using System;
public class Program {
private static string HostingEnvironment => Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
private static bool IsEnvironment(string environmentName) => HostingEnvironment?.ToLower() == environmentName?.ToLower() && null != environmentName;
private static bool Development => IsEnvironment(EnvironmentName.Development);
private static bool Production => IsEnvironment(EnvironmentName.Production);
private static bool Staging => IsEnvironment(EnvironmentName.Staging);
public static void Main(string[] args) { // Your code here }
}
然後你可以簡單地使用屬性
public static void Main(string[] args) {
if (Development){
// Blow up the planet
}
}
謝謝。我最終最終使用了一個更復雜的版本,使用'新的ConfigurationBuilder().AddEnvironmentVariables()'和查找'ASPNETCORE_ENVIRONMENT'。 – user2916547
我似乎沒有在控制檯應用程序中找到buildoptions。 – liang