2017-06-08 50 views
2

使用ASP.NET MVC的核心,我需要設置我的開發環境中使用HTTPS,所以我增加了下面的Main方法的Program.cs:Access環境名ASP.NET核心

var host = new WebHostBuilder() 
       .UseContentRoot(Directory.GetCurrentDirectory()) 
       .UseIISIntegration() 
       .UseStartup<Startup>() 
       .UseKestrel(cfg => cfg.UseHttps("ssl-dev.pfx", "Password")) 
       .UseUrls("https://localhost:5000") 
       .UseApplicationInsights() 
       .Build(); 
       host.Run(); 

如何在此處訪問宿主環境,以便可以有條件地設置協議/端口號/證書?

理想情況下,我只想用CLI來操縱我的託管環境,像這樣:

dotnet run --server.urls https://localhost:5000 --cert ssl-dev.pfx password 

,但似乎並沒有被這樣使用命令行的證書。

回答

12

我認爲,最簡單的方法是閱讀從ASPNETCORE_ENVIRONMENT環境變量的值,並將其與EnvironmentName.Development比較:

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); 
var isDevelopment = environment == EnvironmentName.Development; 
+0

我忘了大約統環境。謝謝! –