我需要在 Program.cs -> Main
方法中访问 IHostingEnvironment.ContentRootPath
。我遵循了互联网上的一些答案,但无济于事。
我的代码如下所示;我可以在 IHostingEnvironment
类型的 Main 方法中声明一个变量,但我不确定应该用什么来初始化它。
public static void Main(string[] args)
{
IHostingEnvironment env;
}
最佳答案
您可以从宿主对象中获取它:
public static void Main(string[] args)
{
var builder = CreateHostBuilder(args);
var host = builder.Build();
var hostingEnvironment = host.Services.GetService(typeof(IHostingEnvironment));
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
关于asp.net-core - 如何访问 Program.cs Main 方法中的 IHostingEnvironment、ContentRoot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63798223/
相关文章:
python-3.x - 由于使用 Docker 的 PostgresDB 连接失败,无法启动 Ap
node.js - Next.js - GetStaticPaths 类型错误
amazon-web-services - 如何更改 CodePipeline 工件行为? (如果可
scala - 如何解决 `/packages cannot be represented as U
angular - Jasmine Angular : How to write unit test
java - Apache Storm 本地集群 "Unable to canonicalize a
c# - 列出目录中的文件夹,其 id 不是 Google Drive API v3 和 C# 中的