c# - 如何在 ConfigureServices 中获取 Development/Staging/production Hosting Environment

标签 c# asp.net-core asp.net-core-mvc

如何在 Startup 的 ConfigureServices 方法中获取 Development/Staging/production Hosting Environment?

public void ConfigureServices(IServiceCollection services)
{
    // Which environment are we running under?
}

ConfigureServices 方法只接受一个IServiceCollection 参数。

最佳答案

您可以在 ConfigureServices 中轻松访问它,只需在 Startup 方法期间将其持久化到一个属性,该方法首先被调用并传入,然后您可以从 ConfigureServices 访问该属性。

public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv)
{
    ...your code here...
    CurrentEnvironment = env;
}

private IWebHostEnvironment CurrentEnvironment{ get; set; } 
 
public void ConfigureServices(IServiceCollection services)
{
    string envName = CurrentEnvironment.EnvironmentName;
    ... your code here...
}

关于c# - 如何在 ConfigureServices 中获取 Development/Staging/production Hosting Environment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548948/

相关文章:

c# - ASP.NET Core 中间件向 Controller 传递参数

c# - 为什么将类移动到 Visual Studio 中的新文件夹中会造成破坏?

c# - .NET 4.6 和 C#6 中是否集成了动态语言运行时 (DLR)?

c# - 如何管理等待工作要做的工作线程?

c# - 为什么未加载ScriptableObject Assets 中的嵌套资源引用?

asp.net-core - .NET Core API 请求与支持的文件类型不匹配

c# - EF Core 3.0 SumAsync 触发聚合函数异常

css - asp.net 5 mvc 6 中的地铁 ui css

c# - 在 'ApplicationDbContext' 上找不到无参数构造函数。在 Asp 内核中

asp.net-core-mvc - 在Mvc6中,WriteAsync函数在哪里?