使用ASP.NET Mvc Core我需要将开发环境设置为使用https,因此我将以下内容添加到Program.cs中的Main
方法中:
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
但似乎没有办法从命令行使用证书。
答案 0 :(得分:75)
我认为最简单的解决方案是从var response = interesting.CreateReply(true);
response.Body = "I'm important!";
response.Priority = MailPriority.High; // No such property?
response.SendAndSaveCopy();
环境变量中读取值并将其与EnvironmentName.Development
进行比较:
ASPNETCORE_ENVIRONMENT
答案 1 :(得分:13)
这是我的解决方案(为ASP.NET Core 2.1编写):
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<IHostingEnvironment>();
if (!hostingEnvironment.IsProduction())
SeedData.Initialize();
}
host.Run();
}
答案 2 :(得分:1)
在.NET Core 3.0中
using System;
using Microsoft.Extensions.Hosting;
然后
var isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development;