Csharp/C#教程:在ASP.NET Core中的Program.Main中访问环境名称分享


在ASP.NET Core中的Program.Main中访问环境名称

使用ASP.NET Mvc Core我需要将我的开发环境设置为使用https,因此我将以下内容添加到Program.cs中的Main方法:

 var host = new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup() .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 

但似乎没有办法从命令行使用证书。

我认为最简单的解决方案是从ASPNETCORE_ENVIRONMENT环境变量中读取值并将其与EnvironmentName.Development进行比较:

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

这是我的解决方案(为ASP.NET Core 2.1编写):

上述就是C#学习教程:在ASP.NET Core中的Program.Main中访问环境名称分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var hostingEnvironment = services.GetService(); if (!hostingEnvironment.IsProduction()) SeedData.Initialize(); } host.Run(); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/985630.html

(0)
上一篇 2021年12月21日
下一篇 2021年12月21日

精彩推荐