ASP.NET Core中Program.Main中的访问环境名称 [英] Access environment name in Program.Main in ASP.NET Core
问题描述
使用ASP.NET Mvc Core,我需要将开发环境设置为使用https,因此我将以下内容添加到Program.cs中的Main
方法中:
Using ASP.NET Mvc Core I needed to set my development environment to use https, so I added the below to the Main
method in Program.cs:
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();
如何在此处访问托管环境,以便可以有条件地设置协议/端口号/证书?
How can I access the hosting environment here so that I can conditionally set the protocol/port number/certificate?
理想情况下,我只想使用CLI来操纵我的托管环境,如下所示:
Ideally, I would just use the CLI to manipulate my hosting environment like so:
dotnet run --server.urls https://localhost:5000 --cert ssl-dev.pfx password
但是似乎没有从命令行使用证书的方法.
but there doesn't seem to be way to use a certificate from the command line.
推荐答案
我认为最简单的解决方案是从ASPNETCORE_ENVIRONMENT
环境变量中读取值并将其与
I think the easiest solution is to read the value from the ASPNETCORE_ENVIRONMENT
environment variable and compare it with EnvironmentName.Development
:
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var isDevelopment = environment == EnvironmentName.Development;
这篇关于ASP.NET Core中Program.Main中的访问环境名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!